Matlab ischar is not recognizing numbers
I am trying to write a robust read command for my application. I want the read code to be able to handle whether the *.txt has a header (title) line or not. The user will create a trajectory file that is always 7 columns wide and an unknown length. The first line can either be a title line for the columns or not. Typically I create my files in excel and then save them using the "Unicode Text" format. I know I could use xlsread() but I would prefer to read from a text file for versatility. Here's my current code: In_fID=fopen([Dir,File],'r'); FirstLine=fgetl(In_fID); model_data=textscan(In_fID,'%f %f %f %f %f %f %f'); if ischar(FirstLine) model_data=cell2mat(model_data); else FirstLine=cell2mat(FirstLine); model_data=cel2mat(model_data); model_data=[FirstLine;model_data]; end Regardless of whether the first line is a header line like "Time x y z r p yw" or the first coordinate of my trajectory (For example: "0 0 0 0 0 0 0") ischar(Firstline) always returns true (1). Does anyone know how I can fix this?
ischar is not the best choice for the check you are trying to do. You can do a check on the first value: In_fID = fopen( [Dir,File] , 'r' ) ; FirstValue = fscanf(In_fID,'%f',1) ; %// this will return empty if the first text does not represent a number if isempty( FirstValue ) Headers = textscan( In_fID , '%s %s %s %s %s %s %s' , 1 ) ; %// read the header titles else fseek( In_fID , 0 , 'bof' ) ; %// Rewind the cursor to the beginning of the file end model_data=textscan(In_fID,'%f %f %f %f %f %f %f'); %// read your data normally fclose(In_fID) ; %// don't forget to close your file ;) This tries to read a numeric value from the file. If it fails (e.g. if the first text in file cannot be converted to a number), it reads a line of text header title (and move the cursor to the next line). If the first reading return an actual value, the program rewind (replace the cursor at the beginning of the file). The last line reads the rest of the file as numeric values.
It is normal that the result of fgetl is a char as long as you didn't reach the EOF. Type doc fgetl at MATLAB Command Window for help; you'll see that the returned value is either a text string or -1. Now, the way the question is asked is not 100% clear what would be the expected behavior... but my guess is: you'd like to know if the first scanned line is a header or not. A good test would be to check the number of converted numeric fields returned by sscanf(FirstLine, '%f'); if is less than the expected count per line, is a big chance that is a header (it may be also a malformed line): N_FIELDS_PER_LINE = 7; %// . . . if numel(sscanf(FirstLine, '%f')) ~= N_FIELDS_PER_LINE %// Line-Is-Header case else %// Line-Is-Data case end;
Why does subplot position change?
Resize the imshow window in matlab
trying to implement knnclassify in matlab for fisheriris dataset
Solving ODEs in MATLAB using the Runga - Kutta Method
How to find the area of only garbage collected in sea using SIFT algorithm in matlab?
rsync: How to NOT apply wildcard excludes recursively
Change window size of a plot in MatLab [duplicate]
Using Multiple Matlab GUIDE Sliders to Adjust Curve Parameters
Are symbolic indexing possible in matlab?
Cannot use dlmread to read the whole tab-delimited file
FFT in Octave/Matlab, Plot cos(x) and approximate with
Distance between broken line and experimental values
multiple plots in a graph in matlab
Get first row of a ascii file (headline) matlab
how to develop LeGall 5/3 filter for reversible watermarking?
matlab: not enough input arguments error