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

Categories

HOME
sbt
math
regex
web-services
anylogic
weight
stack
jbpm
ibm-midrange
splunk
prestashop-1.7
openframeworks
doorkeeper
sql-server-express
openui5
jquery-jtable
axios
lag
accumulo
image-gallery
amadeus
web-audio
eigen3
curve-fitting
spring-restdocs
many-to-many
amazon-cloudfront
precision
addthis
typemock-isolator
regex-group
ruamel.yaml
angular2-databinding
panel-data
react-redux-form
html-select
complex-networks
google-crawlers
argparse
docker-image
json-rpc
weebly
amazon-elasticsearch
azure-management-api
couchbase-lite
multistore
notimplementedexception
jasmine2.0
manual
billing
easyhook
autogen
singlepage
replicate
email-notifications
geneticsharp
blocking
android-sdk-tools
agile-processes
cakephp-2.3
hapi.js
jasny-bootstrap
vlc-android
nuitka
dms
activemodel
vb6-migration
winobjc
cucumber-java
occam-pi
administrator
ane
thinktecture-ident-server
push-api
appv
double-quotes
user-registration
iphone-5
textmatching
emberfire
alt
wso2as
swift-array
sbrk
gdk
knockout-mvc
epoll
monk
aps
windward
azimuth
modalpopupextender
pydatalog
mongo-shell
jquery-mobile-popup
onejar
vlfeat
emitmapper
glulookat
floating-point-conversion
mms-gateway
mosek
jcycle
mknetworkkit
ms-access-97
demoscene
emacs-ecb
sharepoint-timer-job
servletunit
nsindexset
seam-conversation
pinging
dynamic-websites
mud

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App