Info

This question is closed. Reopen it to edit or answer.

Reading a string and retriving data from the string.

2 views (last 30 days)
Michelle
Michelle on 11 Aug 2011
Closed: MATLAB Answer Bot on 20 Aug 2021
I have a str='length=12_height=231_width=123' I want to be able to get the three variables, length=12 height=231 width=123 I have some problems reading because I have many different such strings for example, str1='length=122_height=21_width=12.3' where the variable keeps changing and a decimal place i involved. How will I be able to do that? Please advise and thanks in advance!

Answers (2)

Friedrich
Friedrich on 11 Aug 2011
Hi,
I would use textscan and set the delimiter option to '_'
out = textscan(str1,'%s %s %s','delimiter','_')
After that you will have each variable for his own. But if you really want to create this strings as a variable and assignn the given value, I would do the following
ex_str = strrep(str1,'_',';')
eval(ex_str)

cr
cr on 12 Aug 2011
Another Way:
n = textscan(str,'%s', 'delimiter', '_=');
n = str2double(n{1}(2:2:end))

Community Treasure Hunt

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

Start Hunting!