Used str2num but the result is string.

조회 수: 1 (최근 30일)
Zeynab Mousavikhamene
Zeynab Mousavikhamene 2019년 10월 16일
댓글: Guillaume 2019년 10월 17일
I used str2num to convert string to numeric value but it did not. Any suggeston why?
for jjj=1:length(struct)
splitparam=regexp(struct(jjj).name,'\_','split');
if size(splitparam,2)==2
k_name(jjj)=string(cell2mat(splitparam(1,1)));
k_value(jjj)=str2num(cell2mat(splitparam(1,2)));
elseif (size(splitparam,2)==3)
k_name(jjj)=string(strcat(cell2mat(splitparam(1,1)),'_',cell2mat(splitparam(1,2))));
k_value(jjj)=str2num(cell2mat(splitparam(1,3)));
end
end
  댓글 수: 6
Walter Roberson
Walter Roberson 2019년 10월 17일
The posted code does not create a table. The construction of the table could be the problem
Guillaume
Guillaume 2019년 10월 17일
Actually, according to the screenshot, the table data appears to be string arrays, in which case rather than str2num and str2double, simply use double:
>> double(["123", "456.78"])
ans =
123 456.78
The naming of variables in the given code snippet is appaling. Naming a variable struct is a very bad idea since it will prevent the creation of structure and a better name than the very unimaginative jjj would be advised.
In any case, it would seem that the problem with k_value is that it has been imported incorrectly as a string array. Modifying the import code so that it's imported directly as numbers would be the best course of action, rather than fixing the mess after the fact. To help with that we need an example of the source file (as an attachment).

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by