Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Convert a list of strings 'x[1]', 'x[2]',... into workspace variable arrays.

Subject: Convert a list of strings 'x[1]', 'x[2]',... into workspace variable arrays.

From: Andrew

Date: 22 Jan, 2013 22:19:10

Message: 1 of 5

I need to convert a table of settings into workspace variables with varying sizes. For example, the list appears as such:

Setting name Value
'on_state' 0
'X[1]' -10
'X[2]' -5
'X[3]' 5
'X[4]' 10
'Y[1]' 0
'Y[2]' 1
'Y[3]' 3
'Y[4]' 4
'Default_speed' 4

My goal is to have them in the workspace as:

Name Value
on_state 0
X <4x1 double>
Y <4x1 double>
Default_speed 4

The settings that are meant to be arrays are listed in continuous ascending order, but there may be non-array settings nested between arrays.

Subject: Convert a list of strings 'x[1]', 'x[2]',... into workspace variable arrays.

From: Andrew

Date: 22 Jan, 2013 22:52:10

Message: 2 of 5

UPDATE: I figured it out, but perhaps there is a more elegant way.

Here's what I did:

% Imported data from excel sheet into "untitled" cell array

% Reassign "untitled" cell array to something that makes sense
[rows, cols] = size(untitled); % Get the size of the rows and columns
allcalvals = untitled(1:(rows-1),1:cols); % Read all cals except last line (garbage)
calnames = allcalvals(:,3); % cell array of only the calibration names (strings)
calvals = cell2mat(allcalvals(:,10)); %matrix of calibration values

% Replace left brackets with left parentheses
calnames_nolb = strrep(calnames,'[','(');
% Replace right brackets with right parentheses
calnames_formatted = strrep(calnames_nolb,']',')');

% Populate the workspace with the names and values selected
for i = 1:rows-1
        eval(sprintf('%s = %d',calnames_formatted{i},calvals(i)))
end

I had tried using assignin, but it will not permit creating arrays from strings:
assignin('base','x(3)',5) will produce an error.

Subject: Convert a list of strings 'x[1]', 'x[2]',... into workspace variable arrays.

From: Sadik

Date: 22 Jan, 2013 23:04:10

Message: 3 of 5

Hi Andrew,

I copied and pasted your list starting from 'on_state' directly from your post with no modification into a file named list.txt, did the following and it worked:

fid = fopen('list.txt')
c = textscan(fid,'%s %s')
names = c{1};
values = c{2};
names = strrep(names,'''','');
names = strrep(names,'[','(');
names = strrep(names,']',')')
linesToEval = strcat(names,repmat('=',length(names),1),values);
cellfun(@eval,ans);

Best.

Subject: Convert a list of strings 'x[1]', 'x[2]',... into workspace variable arrays.

From: Sadik

Date: 22 Jan, 2013 23:24:13

Message: 4 of 5

Sorry, the last line should have been

cellfun(@eval,linesToEval);

Best.

Subject: Convert a list of strings 'x[1]', 'x[2]',... into workspace variable arrays.

From: Andrew

Date: 23 Jan, 2013 16:49:09

Message: 5 of 5

"Sadik " <sadik.hava@gmail.com> wrote in message <kdn72t$9of$1@newscl01ah.mathworks.com>...
> Sorry, the last line should have been
>
> cellfun(@eval,linesToEval);
>
> Best.


Thanks Sadik. This is a bit cleaner than mine, and it's nice to see how to use the "@function" code.

AP

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us