필터 지우기
필터 지우기

str2num returning value while the preferred str2double returning NaN...

조회 수: 5 (최근 30일)
Manuel S Mathew
Manuel S Mathew 2021년 4월 1일
답변: Rik 2021년 4월 1일
New to MATLAB, so maybe this is a stupid question. I have the following script in MATLAB to run a python script and return a value (3.4763, for eg). When I use str2num like below, I get a the value in p. However, MATLAB suggests to use str2double. When I do that, I get NaN instead of the value. Please help...
w = 16;
systemCommand = ['python fun.py ',num2str(w)];
[stat, res] = system(systemCommand);
p = str2num(res);
What python returns from execution is '[3.4763]' in the variable res.

채택된 답변

Rik
Rik 2021년 4월 1일
This is another difference:
str2double('evalc(''exit'')')
ans = NaN
str2num('evalc(''exit'')')
%will exit Matlab
You should parse the result to single values. In your case simply removing the brackets will do, but this should be more robust:
str='[3.14159 1.00e-001]';
str2double(regexp(str,'[.0-9eE\-]*','match'))
ans = 1×2
3.1416 0.1000

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Call Python from MATLAB에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by