X = ["3.716,3.711,3.719,3.714,3.714,3.711,3.722,3.712,3.715,3.715,3.717,3.721,3.713,3.714,0.000"];
Y = str2double(X);
I am trying to convert the above string to double. However, using above code, the Y value is 'NaN'.
'str2num(X)' gives appropriate/required results.
Y = [3.71600000000000 3.71100000000000 3.71900000000000 3.71400000000000 3.71400000000000 3.71100000000000 3.72200000000000 3.71200000000000 3.71500000000000 3.71500000000000 3.71700000000000 3.72100000000000 3.71300000000000 3.71400000000000 0]
However, 'str2num' is not supported in code generation. Is there any alternative?

 採用された回答

Stephen23
Stephen23 2023 年 1 月 30 日

1 投票

X = "3.716,3.711,3.719,3.714,3.714,3.711,3.722,3.712,3.715,3.715,3.717,3.721,3.713,3.714,0.000";
str = X{1}; % character vector
idx = str==',';
idy = diff([true,idx,true]);
idb = find(idy<0);
ide = find(idy>0)-1;
num = numel(idb);
tmp = cell(num,1);
for k = 1:num
tmp{k} = str(idb(k):ide(k));
end
vec = str2double(tmp)
vec = 15×1
3.7160 3.7110 3.7190 3.7140 3.7140 3.7110 3.7220 3.7120 3.7150 3.7150

8 件のコメント

Praveen Kumar
Praveen Kumar 2023 年 1 月 30 日
Thanks, working,
Praveen Kumar
Praveen Kumar 2023 年 2 月 1 日
@Stephen23 Is there any work around to replace str2double or improve its performance?
The performance is largely affected by 'str2double'; which takes 6.3s out of 8.9s elapsed time (obtained from Profiler)
Steven Lord
Steven Lord 2023 年 2 月 1 日
How is the X variable created in your application? If you're reading it in from a file there may be a way to have the function that imports the data automatically convert it to double instead of importing it as a string array.
Praveen Kumar
Praveen Kumar 2023 年 2 月 2 日
編集済み: Praveen Kumar 2023 年 2 月 2 日
The X variable is read from excel (.csv) file. Please find below screenshot of the original file opened in excel.
The CSV file is imported as output type 'table' which contains X and other variables (using readtable).
Stephen23
Stephen23 2023 年 2 月 2 日
編集済み: Stephen23 2023 年 2 月 2 日
"The X variable is read from excel (.csv) file."
A CSV file is just a text file. CSV files exist completely independently from MS Excel.
"(using readtable)"
Probably you should fix the file importing, this is probably easier than fiddling around with text afterwards.
Please upload a sample data file by clicking on the paperclip button.
Praveen Kumar
Praveen Kumar 2023 年 2 月 2 日
Please find the sample csv file in the attachment.
One of the objective is to extract the X (containing 15 values separated by comma) in to a matrix.
As per the attached file example, X will 13 x 15 matrix in the end.
Stephen23
Stephen23 2023 年 2 月 2 日
編集済み: Stephen23 2023 年 2 月 2 日
M = readmatrix('matlab_error_test.csv', 'Range',2, 'Delimiter',{',','"'}, 'ConsecutiveDelimitersRule','join')
M = 13×25
512.0000 0 7.6800 0.0300 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -25.0000 3.0000 58.0000 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -34.0000 3.0000 58.0000 0 0 0 512.0000 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -30.0000 18.0000 0 0 0 0 512.0000 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -64.0000 20.0000 0 0 0 0 512.0000 0 0 0 0 0 0 0 0 0 0 6.9120 30.9800 61.6990 53.2470 31.4850 7.0000 0 -15.0000 2.0000 0 0 0 0 0 0 3.7710 3.7770 3.7780 3.7800 3.7800 3.7780 3.7500 3.7720 3.7790 3.7810 3.7750 3.7830 3.7810 3.7740 0 31.0000 31.0000 32.0000 31.0000 32.0000 30.0000 30.0000 30.0000 0 0 3.7710 3.7770 3.7790 3.7800 3.7790 3.7790 3.7500 3.7720 3.7790 3.7810 3.7750 3.7830 3.7810 3.7740 0 31.0000 31.0000 32.0000 31.0000 32.0000 31.0000 30.0000 30.0000 0 0 3.7710 3.7770 3.7780 3.7810 3.7790 3.7780 3.7500 3.7730 3.7790 3.7810 3.7750 3.7830 3.7810 3.7740 0 31.0000 31.0000 32.0000 31.0000 32.0000 30.0000 30.0000 30.0000 0 0 3.7710 3.7770 3.7780 3.7810 3.7790 3.7790 3.7500 3.7720 3.7790 3.7810 3.7750 3.7830 3.7810 3.7740 0 31.0000 31.0000 32.0000 31.0000 32.0000 30.0000 30.0000 30.0000 0
X = M(:,2:16)
X = 13×15
0 7.6800 0.0300 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 6.9120 30.9800 61.6990 53.2470 31.4850 3.7710 3.7770 3.7780 3.7800 3.7800 3.7780 3.7500 3.7720 3.7790 3.7810 3.7750 3.7830 3.7810 3.7740 0 3.7710 3.7770 3.7790 3.7800 3.7790 3.7790 3.7500 3.7720 3.7790 3.7810 3.7750 3.7830 3.7810 3.7740 0 3.7710 3.7770 3.7780 3.7810 3.7790 3.7780 3.7500 3.7730 3.7790 3.7810 3.7750 3.7830 3.7810 3.7740 0 3.7710 3.7770 3.7780 3.7810 3.7790 3.7790 3.7500 3.7720 3.7790 3.7810 3.7750 3.7830 3.7810 3.7740 0
Praveen Kumar
Praveen Kumar 2023 年 2 月 2 日
Thank you very much. Understood!

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

その他の回答 (2 件)

Askic V
Askic V 2023 年 1 月 30 日

2 投票

Try this:
X = "3.716,3.711,3.719,3.714,3.714,3.711,3.722,3.712,3.715,3.715,3.717,3.721,3.713,3.714,0.000";
newStr = split(X,',')
Y = str2double(newStr)

1 件のコメント

Praveen Kumar
Praveen Kumar 2023 年 1 月 30 日
Thanks for the quick response. Even though the code works, 'split' is not supported in C/C++ code generation.

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

Askic V
Askic V 2023 年 1 月 30 日
編集済み: Askic V 2023 年 1 月 30 日

1 投票

I would also like to suggest this solution:
X = "3.716,3.711,3.719,3.714,3.714,3.711,3.722,3.712,3.715,3.715,3.717,3.721,3.713,3.714,0.200";
f = strfind(X, ","); % find indices of delimiter
Y = zeros(1, numel(f)+1); % initialize output array
X_char = convertStringsToChars(X);
j = 1;
for i = 1:numel(f)
start = j;
Y(i) = str2double(X_char(j:f(i)-1));
j = f(i)+1;
end
Y(end) = str2double(X_char(f(i)+1:end)); % add last element
Y
Y = 1×15
3.7160 3.7110 3.7190 3.7140 3.7140 3.7110 3.7220 3.7120 3.7150 3.7150 3.7170 3.7210 3.7130 3.7140 0.2000

カテゴリ

ヘルプ センター および File ExchangeData Type Conversion についてさらに検索

質問済み:

2023 年 1 月 30 日

編集済み:

2023 年 2 月 2 日

Community Treasure Hunt

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

Start Hunting!

Translated by