Matlab turns char into double on cluster.

1 回表示 (過去 30 日間)
Michael Stollenwerk
Michael Stollenwerk 2022 年 3 月 30 日
コメント済み: Stephen23 2022 年 4 月 5 日
On the cluster I run
matlab -nodesktop -r "program 1" > program_1.out 2>&1
where the first lines of code in "program" look like this
function program(dtaset_id)
addpath(genpath('../..'))
if isempty(gcp('nocreate'))
parpool(maxNumCompThreads,'IdleTimeout', 300)
end
whos
load(strcat('dataset_', dtaset_id, '.mat'))
whos
dtaset_id = str2num(dtaset_id);
The strange thing is that the first "whos" reports "dtaset_id" to be a char, and the second whos reports it to be a double, causing an error in the last line.
How and why does this happen?

採用された回答

Voss
Voss 2022 年 3 月 30 日
It may be that the mat file you load contains a variable called dtaset_id, which is a double. When you load the mat file that value overwrites the previous value of dtaset_id in your workspace.
Here I've attached a mat file with a double dtaset_id to demonstrate.
dtaset_id = '0';
whos('dtaset_id') % char
Name Size Bytes Class Attributes dtaset_id 1x1 2 char
load(strcat('dataset_', dtaset_id, '.mat')) % overwrites dtaset_id with value from mat file
whos('dtaset_id') % now double
Name Size Bytes Class Attributes dtaset_id 1x1 8 double
  2 件のコメント
Michael Stollenwerk
Michael Stollenwerk 2022 年 4 月 5 日
Of course! That's it. Sorry, this one had me very puzzeled and as always in those cases its hiding in plain sight. Thanks!
Stephen23
Stephen23 2022 年 4 月 5 日
This is one reason why it is recommended to always LOAD into an output variable:
S = load(..);

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGet Started with MATLAB についてさらに検索

タグ

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by