Clear Filters
Clear Filters

String handling questions

1 view (last 30 days)
Raymond Le
Raymond Le on 16 Apr 2011
I have 2 questions regarding about strings and I would like to get help. 1. How to handle the " ' " in Matlab? - The syntax set_param function is set_param('Model_Name/Block_Name', 'Value', 'data') and I would like to set multiple Block_Name's Value (Block_Name is named as K(i), where i = 1 to N - I tried set_param('Model_Name/K(' & num2str(i) & ')', 'Value', 'data') and set_param('Model_Name/K(' num2str(i) ')', 'Value', 'data') but both failed
2. I have a list of string like [1.21321421412421512312312521131232] and try to convert in to number using num2str() can only convert certain digit, I tried with format long and format longeng but they still cannot handle all of the digits. Is there anyway to handle this?

Answers (2)

Paulo Silva
Paulo Silva on 16 Apr 2011
set_param(['Model_Name/K(' num2str(i) ')'], 'Value', 'data')
  2 Comments
Raymond Le
Raymond Le on 16 Apr 2011
Thank you very much for helping me out. If I have an array
a = [1 23 42 45 42 54 56 43 45] and I would like to set in to an attribute of a block let set Value which only take single value
I tried:
set_param(['Model_Name/K(' num2str(i) ')'], 'Value', a(i+1))
set_param(['Model_Name/K(' num2str(i) ')'], 'Value', 'a(i+1)')
set_param(['Model_Name/K(' num2str(i) ')'], 'Value', ['a(i+1)'])
but I only get for Value of K(0) = a( , instead of Value of K(0) = 1. Then the program errors out.
Walter Roberson
Walter Roberson on 16 Apr 2011
set_param(['Model_Name/K(' num2str(i) ')'], 'Value', num2str(a(i+1)) )
Or you might find you prefer the style
set_param( sprintf('Model_Name/K(%d)', i), 'Value', sprintf('%d', a(i+1)) )

Sign in to comment.


Walter Roberson
Walter Roberson on 16 Apr 2011
The closest you can get to
1.21321421412421512312312521131232
in MATLAB is
1.21321421412421504015810569399036467075347900390625
which is accurate to within
0.0000000000000002220446049250313080847263336181640625
In order to represent 1.21321421412421512312312521131232 more accurately, you need to use the Symbolic Toolbox,
digits(35);
A = sym('1.21321421412421512312312521131232');
However, to use the number meaningfully you would pretty much need to convert the rest of your numbers to symbolic as well as otherwise intermediate calculations might not retain enough accuracy.

Categories

Find more on Characters and Strings in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!