logical data some time is not 0/1

5 Ansichten (letzte 30 Tage)
Luca Re
Luca Re am 19 Jul. 2023
Kommentiert: Luca Re am 19 Jul. 2023
function startupFcn(app, setting)
app.Calling_Predy2=settings;
app.UITable.ColumnName=["Strategy";"On";"Underlying";"Trading";"OOS";"Sipp.source";"Slippage";"Type";"Horizon";"Filter skip";"Static Size";"Min Size";"Max Size";"Min AvgTrade";"Select"];
app.UITable.ColumnEditable=[false,true,false,true,true,true,true,true,true,true,true,true,true,true,true];
cat = categories(app.Trading);cat2=categories(app.SlippSource);cat3=categories(app.Type);cat4=categories(app.Horizon);cat5=categories(app.SiNo);
colu={'char' 'logical' 'char' {cat{:}} 'char' {cat2{:}} 'char' {cat3{:}} {cat4{:}} {cat5{:}} 'char' 'char' 'char' 'char' 'logical'};
app.UITable.ColumnFormat=colu;
app.SaveAsButton.Visible="off";
app.SaveButton.Visible="off";
app.Edit_Button.Visible='off';
...
..
colu={'char' 'logical'.. ===> i set "logical" the second column of table
then i go in app designer and i change value of "On" (pics 1.png), i save it
and i check it using import(file)
and i see similar to this (see 2.png)
i see true and false.... (but having set the field as logical they should all be 0/1)

Akzeptierte Antwort

Adam Danz
Adam Danz am 19 Jul. 2023
Bearbeitet: Adam Danz am 19 Jul. 2023
2.png indicates that the data is a 82x15 string where some values in col 2 are in the form "true"/"false" whereas other values in col 2 are in the form "0". How are you reading in this data? You can convert this mix of strings to a logical vector using one of the options below.
Option 1: string comparison
str = ["false","1","true","0"];
bool = ismember(lower(str),["true","1"])
bool = 1×4 logical array
0 1 1 0
However, if any unexpected values such as "2" appear in the string, this will convert them to false. To validate the conversion, this line will throw an error if there are any unexpected strings.
assert(all(ismember(lower(str),["true","false","1","0"])),"String values must represent boolean values.")
Option 2: Class conversion
str = ["false","1","true","0"];
bool = arrayfun(@(s)logical(str2num(s)),str)
bool = 1×4 logical array
0 1 1 0
However, if any unexpected values such as "2" or "F" appear in the string, this may either throw an error or silently return a logical value. To validate the conversion, use the same assert validation in option 1.
Option 3: Specify varopts when reading the data
If these data are read into MATLAB, you could use T = readtable(filename,opts) and set opts using setvaropts. In setvaropts you can specify,
opts = setvaropts(opts,'mybool',...
"TrueSymbols",["true","1"],...
"FalseSymbols",["false","0"]);
This is demonstrated well in this answer by @Jeremy Hughes.
  3 Kommentare
Adam Danz
Adam Danz am 19 Jul. 2023
Thanks for the explanation. This is because the logical values are converted to strings:
string(true)
ans = "true"
string(false)
ans = "false"
Instead of storing the data in a string array, is there a reason you could not use a table in which case the column that stores these values can be logical?
Luca Re
Luca Re am 19 Jul. 2023
i want to use a table because i am displaying data in table format in app designer
I can however convert the data before it is saved to app.MyTable=app.UITable.Data;

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Timetables finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by