I have a function that reads data from a text file and returns a structure with the data. One field of the data should be logical and one field should be double. When I run this function one time, the field that should be logical is logical. Great. But when I run the function again, the data type of the field that should be logical is double. Is there any explanation for this?
The first thing I did was try to debug it by putting a breakpoint at the line that assigns the logical data to output. To my surprise, simply the act of placing a breakpoint here fixes the problem. This is true even if I make the breakpoint conditional and the condition always evaluates to false.
I've also discovered that the problem goes away when I clear the function using the "clear" command.
Here is my function:
function Data = readData2(DataFile)fp = fopen(DataFile,'r');Data.boolVar = getVariable(fp);Data.scalarVar = getVariable(fp);fclose(fp);endfunction val = getVariable(fp_in)keep_going = true;while keep_going txt = fgetl(fp_in); if strcmp(txt(1),'>') val = parseData(txt); keep_going = false; endendendfunction val = parseData(str)if isempty(strfind(str,'<')) str = [str ' <'];endtoks = regexp(regexp(str,'(?<=> ).*(?= <)','match'),'\S+','match');toks = toks{1};for i = 1:length(toks) valstr = toks{i}; if strcmp(valstr,'T') val(1,i) = true; elseif strcmp(valstr,'F') val(1,i) = false; else val(1,i) = str2double(valstr); endendend
And the text file it is reading:
* data_file.txt*--------------------------------------------* Comment Comment Comment*------------------------------------------------* Comment Blah Blah*------------------------------------------------* boolean variable 1> T T T T*** Comments*** 3 more variables> 0.4 -0.3 -0.3 0.6 > 0.4 0.9 -0.4 -0.8 > 0.7 0.1 0.2 0.3** another variable> 3* * More comments
Here is a tester script that demonstrates my problem:
clear allclcislogicaldisp = @(x) disp([' isa(Data.boolVar,''logical'') = ' num2str(isa(x,'logical'))]);isdoubledisp = @(x) disp([' isa(Data.boolVar,''double'') = ' num2str(isa(x,'double'))]);Data = readData2('data_file.txt');disp('First pass:');islogicaldisp(Data.boolVar)isdoubledisp(Data.boolVar)Data = readData2('data_file.txt');disp('Second pass - converted to double!?!?:');islogicaldisp(Data.boolVar)isdoubledisp(Data.boolVar)Data = readData2('data_file.txt');disp('Third pass - just to make sure...:');islogicaldisp(Data.boolVar)isdoubledisp(Data.boolVar)dbstop in readData2 at 34 if falsedisp('Fourth pass - this time add an inactive breakpoint:');Data = readData2('data_file.txt');islogicaldisp(Data.boolVar)isdoubledisp(Data.boolVar)disp(' What?? ^^^^^^^^^');dbclear in readData2disp('Fifth pass - deactivate breakpoint and try again:');Data = readData2('data_file.txt');islogicaldisp(Data.boolVar)isdoubledisp(Data.boolVar)Data = readData2('data_file.txt');disp('Sixth pass - just to be sure...:');islogicaldisp(Data.boolVar)isdoubledisp(Data.boolVar)clear readData2Data = readData2('data_file.txt');disp('7th pass - cleared readData2 function:');islogicaldisp(Data.boolVar)isdoubledisp(Data.boolVar)
And here is the result of the test script:
First pass: isa(Data.boolVar,'logical') = 1 isa(Data.boolVar,'double') = 0Second pass - converted to double!?!?: isa(Data.boolVar,'logical') = 0 isa(Data.boolVar,'double') = 1Third pass - just to make sure...: isa(Data.boolVar,'logical') = 0 isa(Data.boolVar,'double') = 1Fourth pass - this time add an inactive breakpoint: isa(Data.boolVar,'logical') = 1 isa(Data.boolVar,'double') = 0 What?? ^^^^^^^^^Fifth pass - deactivate breakpoint and try again: isa(Data.boolVar,'logical') = 0 isa(Data.boolVar,'double') = 1Sixth pass - just to be sure...: isa(Data.boolVar,'logical') = 0 isa(Data.boolVar,'double') = 17th pass - cleared readData2 function: isa(Data.boolVar,'logical') = 1 isa(Data.boolVar,'double') = 0
Any idea what is going on here?
Thanks!
Best Answer