Used str2num but the result is string.

1 次查看(过去 30 天)
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