Test the variable type from file input

I recall a way in Verilog to test a char read in from a file (like a buffer, I believe) and then based on what that char is, proceed in a certain way.
My current file reading uses fscanf and reads in only numbers, but the users here sometimes user True/False for a line, and sometimes 0/1. An example of a line could be either:
Fruit Bananas True Potatoes False
~~ or ~~
Fruit Bananas 1 Potatoes 0
I'm trying to migrate toward 0/1, since I don't think Matlab recognizes True/False like python can. So I want to be able to read in either file type. If the scan returns a number (after the fruit type), then I proceed with numbers, if it returns a char or string, then I proceed for that. Does anyone have suggestions? Thanks!
&nbsp
EDIT: My actual input file looks more like this: (a mixture)
VAR1 55.6
VAR2 23
VAR3 0
VAR4
VAR5
VAR6 -999
VAR7 True
VAR8 False
VAR9 0
VAR10 1

 Akzeptierte Antwort

per isakson
per isakson am 21 Aug. 2014
Bearbeitet: per isakson am 21 Aug. 2014

0 Stimmen

Another approach
fid = fopen('bananas.txt','r');
cac = textscan( fid, '%s%s%s%s%s' );
fclose( fid )
isb = cellfun( @(str) logical( eval( lower( str ) ) ), cac{3} );
where bananas.txt contains the two lines
Fruit Bananas True Potatoes False
Fruit Bananas 1 Potatoes 0
Inspect isb
>> whos isb
Name Size Bytes Class Attributes
isb 2x1 2 logical

5 Kommentare

Maybe you want to make sure that the column in questions only contains legal strings. eval is a bit scary since it tries to evaluate anything.
assert( all( ismember( lower(cac{3}), {'1','0','true','false'} ) ) ...
katerina
katerina am 21 Aug. 2014
Bearbeitet: per isakson am 22 Aug. 2014
I'm getting an error from this:
hold = textscan(file,'%s%s');
assign = cellfun(@(str) logical(eval(lower(str))),hold{2});
Here's the error:
Error using get_data>@(str)logical(eval(lower(str))) (line 15)
Error: Expression or statement is incomplete or incorrect.
Error in get_data (line 15)
assign = cellfun(@(str) logical(eval(lower(str))),hold{2});
My actual input file contains both numbers and T/F strings for the values of data. What could be the source of error here? Also, in hold{2} there are some empty slots where 0 was read in. Not sure if this could produce the error.
VAR4 and VAR5 have no values. That causes the error.
>> z = eval( lower('') )
Error: Expression or statement is incomplete or incorrect.
per isakson
per isakson am 22 Aug. 2014
Bearbeitet: per isakson am 22 Aug. 2014
My "smart" on-liner doesn't work and cannot be fixed to work with empty and numerical values.
Lessons learned:
  • Don't use specialized on-liners.
  • Backward engineering of a simple example often results in erroneous requirements and consequently an answer, which doesn't apply to the real problem
Proposal: post a new question.
per isakson
per isakson am 22 Aug. 2014
Bearbeitet: per isakson am 22 Aug. 2014
  • "I'm trying to migrate toward 0/1" &nbsp Why not stick with True/False? That makes the file more readable in the editor. What if '1' means numerical 1 for some other variable, e.g. VAR1
  • "I don't think Matlab recognizes True/False like python" &nbsp I don't know Python well enough to understand the meaning of this statement.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

the cyclist
the cyclist am 21 Aug. 2014
Bearbeitet: the cyclist am 21 Aug. 2014

0 Stimmen

There are ischar() and isnumeric() functions in MATLAB, to identify particular variable types, as well as the class() function.
Also, MATLAB will recognize true and false as logical values.

2 Kommentare

katerina
katerina am 21 Aug. 2014
so if I read in "true" from a file, is there a value type I can use so that it will convert to 1 or 0?
katerina
katerina am 21 Aug. 2014
I realize I could just read in the string and convert it if needed, but I'd like a clean way to discriminate between reading a number or "True"/"False".

Melden Sie sich an, um zu kommentieren.

Kategorien

Community Treasure Hunt

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

Start Hunting!

Translated by