logical data some time is not 0/1
2 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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)
0 Commenti
Risposta accettata
Adam Danz
il 19 Lug 2023
Modificato: Adam Danz
il 19 Lug 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"])
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)
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"]);
3 Commenti
Adam Danz
il 19 Lug 2023
Thanks for the explanation. This is because the logical values are converted to strings:
string(true)
string(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?
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Timetables in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!