我有char型別的變數'txt',我想把它變成一個整數陣列:
txt = '-1 1 -3 -1? -7 -4 -2 -3 -1 -2? -2 -24 -3 -1 -2 -2 -1 -1? -3 -1 0 -1 -2 -3 -4 -5? -5 -2 -1 -1 -2 -11-15-27? -5 -7 -30-19-16-18-19-18? -5 -4 -28-28- 19-13? -4 -3 -13 -6
'
大小(txt)
答案=
1 160
num2str(txt)
答案=
'-1 1 -3 -1
-7 -4 -2 -3 -1 -2
-2 -24 -3 -1 -2 -2 -1 -1
-3 -1 0 -1 -2 -3 -4 -5
-5 -2 -1 -1 -2 -11-15-27
-5 -7 -30-19-16-18-19-18
-5 -4 -28-28-19-13
-4 -3 -13 -6
'
str2num(txt)
答案=
[]
double(txt) 也回傳一個數字陣列,但它們與 txt 陣列中的數字不同。如何將 txt 陣列轉換為整數?
uj5u.com熱心網友回復:
您必須首先在代碼中找到并消除“換行符”字符。換行符的 ASCII 碼是 10。因此,請嘗試使用此代碼查找、消除它們并用空格替換它們。
arr= #read the text;
arr(double(arr)==10) = ' ';
a = find(arr=='-')-1;
a = a(a>0);
indcs = a(find(arr(a) ~= ' '));
for i=1:length(indcs)
arr = [arr(1:indcs(i)), ' ', arr(indcs(i) 1:end)];
indcs = indcs 1;
end
x = str2num(arr)
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/432372.html
上一篇:MATLAB編譯
下一篇:在2個條件下拆垛
