how to execute str2num or str2double in matlab?

I am having trouble using commands str2num.
I have a character
t='2'
'u1'
'u2 '
I used the command p=str2double(char(t)). Right now if I use str2num, a null array appears in the output whereas using str2double gives NaN in output. While searching about these I learned that these character values are not converted to numbers.
I wanted p to store values [2, 1, 2]. How can I do that? Thanks for reading...

4 件のコメント

Walter Roberson
Walter Roberson 2018 年 5 月 9 日
Is t a char vector that might at different times have '2' or 'u1' or 'u2 ' ? Or is t a cell array of character vectors that has those three character vectors in it? Or is t a single character vector '2 u1 u2 ' ?
In the number portion, is it necessary to recognize negative values? Is it necessary to recognize decimal points and fractions? How about exponential numbers? How about complex values?
MEENAL SINGHAL
MEENAL SINGHAL 2018 年 5 月 9 日
編集済み: per isakson 2018 年 5 月 9 日
Thank you for responding. t here is a 3x1 cell array containing 3 characters '2' 'u1' 'u2'. My code is:
function sol=AdomPoly(expression,nth)
% Coded by H. F. & H. A., Feb, 2011
Ch=char(expand(expression));
s=strread(Ch, '%s', 'delimiter', '+');
for i=1:length(s)
t=strread(char(s(i)), '%s', 'delimiter', '*()expUlogsinh');
t=strrep(t,'^','*');
if length(t)~=2
p=str2double(char(t));
sumindex=sum(p)-p(1);
else
sumindex=str2double(char(t));
end
list(i)=sumindex ;
end
A='';
for j=1:length(list)
if nth==list(j) ;
A=strcat(A,s(j),'+');
end
end
N=length(char(A))-1;
F=strcat ('%',num2str(N),'c%n');
sol=sscanf(char(A),F);
end
Further the expression = (u0+u1+u2)^2; nth=2; where u0, u1, u2 are symbolic variables.
per isakson
per isakson 2018 年 5 月 9 日
編集済み: per isakson 2018 年 5 月 9 日
Hint:
t = { '2' 'u1' 'u2 '};
c = regexp( t, '[\d ]+', 'match' );
p = cellfun( @str2double, c )
outputs
p =
2 1 2
MEENAL SINGHAL
MEENAL SINGHAL 2018 年 5 月 9 日
編集済み: MEENAL SINGHAL 2018 年 5 月 9 日
Thanks @per isakson for the hint [and for editing my question :)]. It works individually in command prompt but gives a error saying "set UniformOutput to false" when I run the same with the complete code.

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

 採用された回答

Stephen23
Stephen23 2018 年 5 月 9 日
編集済み: Stephen23 2018 年 5 月 9 日

0 投票

Simple, and possibly the most efficient way:
>> t = {'2','u1','u2 '};
>> sscanf([t{:}],'%du')
ans =
2
1
2

2 件のコメント

MEENAL SINGHAL
MEENAL SINGHAL 2018 年 5 月 9 日
Thanks Stephen Cobeldick for answering. The problem is solved for
t='2'
'u1'
'u2 '
But what if I have
t= 'u0*2 '
'u1*2 '
'u2*2 '
Now I want 'p' to store [0*2 1*2 2*2] ,i.e., [0 2 4]. How can I do that?
Stephen23
Stephen23 2018 年 5 月 9 日
編集済み: Stephen23 2018 年 5 月 9 日
@MEENAL SINGHAL: Perhaps you are asking about how to write a maths parser, which in general is not a trivial task. What operators do you expect to have? For simple multiplication:
>> t = {'u0*2','u1*2','u2*2'};
>> v = sscanf([t{:}],'u%d*%d');
>> v(1:2:end).*v(2:2:end)
ans =
0
2
4

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

その他の回答 (0 件)

カテゴリ

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

質問済み:

2018 年 5 月 9 日

編集済み:

2018 年 5 月 9 日

Community Treasure Hunt

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

Start Hunting!

Translated by