Reading txt file, organizing data and saving formatted data into excel

1 view (last 30 days)
Greetings, I am trying to format a raw data feedback from my servodriver to ake it as readable one and might plot it later too. The problem is that the data is not consistent. By consistent I mean there are many errors in data and sometime two data are merged together. So manually , first I seperated the data as the position data (6130.0438232) always has 12 characters including '.'dot and the position data is followed by voltage data (0.0908446) which has 9 character(including '.' dot). After doing for all I did seperated them into different columns. Is it easy to do in Matlab? I am new to matlab. It urgent for me to process these data as the submission is near so shall I learn matlab now to do my job? Can i do it in few days? I have understanding of programming like C# and C++. Any suggestion and replies please.
  3 Comments
Abinash Baral
Abinash Baral on 30 Mar 2019
Edited: Abinash Baral on 30 Mar 2019
Hi, thank you for your reply. Actually I am quite new to interface of matlab which I find different than other programs like C# or C++ where we write all the code and run later. I have attached the files now. About the source of data , I tried to solve but the data could not be error free completely so I have to run some program that removes the error and keep the interpolated values in place of errors.
dpb
dpb on 30 Mar 2019
Well, you can write all the code in the Matlab editor and run it later if you wish...just that w/ ML you can try snippets of code at the command line to try things that you have to do the edit/compile/link/run sequence in the compiled languages.
Of course, with IDEs often that process is simply a mouse click or keystroke macro invocation, but you have that same feature in the ML editor, too...
It really isn't all that different..
The real difference in Matlab is that operations are vectorized so you can avoid most loop constructs for many (particularly numerical) problems and that it has so many prepackaged functions to do higher level operations. Unfotrunately, for your case, you probably can't take too much advantage of that because you've got to find where the anomalies are in the input file on a character basis.
There's another poster with different specifics but somewhat similar problem right now that might give you some ideas...altho I don't think the textscan route is going to help you, regexp might but I'm not adept-enough in its syntax to have any specific suggestions.
To get started on the read and and character search, to suck up the file into memory as a character array, control characters and all should be quite a familiar-looking code...
fid=fopen('RawData.txt','r');
raw=fread(fid,'*char');
fid=fclose(fid);
Now you have a char() array that can search for the various substring pieces sequentially and build a new repaired string array in memory. Note you can treat characters in Matlab by either their character representation as '!' or as numeric (to search for control characters, for example).
If you have an idea of how you would solve your problem in C, you can essentially mimic that in Matlab. For most problems that isn't effective use of ML, but in this case it likely is.

Sign in to comment.

Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!