Used str2num but the result is string.
1 view (last 30 days)
Show older comments
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 Comments
Walter Roberson
on 17 Oct 2019
The posted code does not create a table. The construction of the table could be the problem
Guillaume
on 17 Oct 2019
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).
Answers (0)
See Also
Categories
Find more on Data Type Conversion in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!