### matlab

#### 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;

### Related Links

MATLAB SIMULINK - Add Sine wave to Wav file

Ways to Compute Spectrum Matlab

Find all zero(s) in matrix in matlab [duplicate]

Automatically changing block variables and collecting data from a model

Simulink running on multiple cores (or not) for a given model

index exceed matrix dimensions

How to convert UTF-8 to Windows-1252 in Matlab?

Writing a Matlab constraint unit test

quasi random set and multivariate normal distribution matlab

How do you convert a matrix to a cell array of equivalent size? [duplicate]

How do I create a 50x3 cell array from a 50x3 matrix. Keep just getting a 1x1 cell array with a 50x3 submatrix?

How to “project out” some dimensions of an n-d array?

How to Convert Vector to String in Matlab

Change axis Label in contourf

MATLAB: Approximate tomorrow's temperature with 2nd, 3rd and 4th polynomial using the Least Squares method

DCT rows instead of columns