How do you assign a variable name to an array at specific location

Hi, I am looking to create a matrix of filenames. I have a for loop that goes through each data set and I can get the variable character to pick up the filename accurately. However when I try to put in a specific location in the array I just get errors. FYI - I am still working on R2012a Student edition.
Example of code.
for i=1:10
filename = 'B01' % this is calculated from another for loop so cant ...
% just use 'B01' later on, and will change for every i.
matrix{i} = filename;
end
Hope what I am trying to do is clear. I want a final column matrix similar to [B01, B02, B03...] Not really as simple as relating i to the number in the filename.
Regards

2 Comments

Note that you should not use i or j as your loop variable, as this is the name of the inbuilt imaginary unit .
I do not get any errors when I run your code:
for k=1:10
filename = 'B01';
matrix{k} = filename; %#ok<SAGROW>
end
If you really are getting an error, there must be something else that you are doing that you have not shown us. Do you define matrix somewhere? What is the exact error message that you get?

Sign in to comment.

 Accepted Answer

Two possible solutions...
Keep the names in a cell array :
mat = cell(10,1);
for k=1:10
fnm = 'B01';
mat{k} = fnm; %#ok<SAGROW>
end
Keep the names in a character array :
mat = repmat(' ',10,1);
for k=1:10
fnm = 'B01';
mat(k,1:numel(fnm)) = fnm; %#ok<SAGROW>
end

More Answers (1)

Your problem above is you're not handling the character array in the assignment...
m(i,:)=filename:
will solve that but creates more "issues" in that you have to always carry around that second index--otherwise m(i) will simply refer to one character in the array counted linearly.
Use cellstrings instead --
m(i)={filename};
Then m(i) will return the full cellstring; when you're ready to use it in, say, fopen or other places that expect a character string, then use the curlies "{}" to reference the cell or explicitly cast it
fid=fopen(m{i});
or
fid=fopen(char(m(i));
See the "Getting Started" section on character arrays, cellstrings and cell arrays for the details...

5 Comments

This depends on code that comes before what the OP gives us: we know nothing about how matrix is defined, or if it is at all.
Well, I believe we can assume OP is correct when he says ...I can get the variable character to pick up the filename... so I made the correction of the most obvious problem newbies have using character arrays.
He also says he's not using a fixed character string but some other function to return the desired string. Now, if that is a cellstring on return only the second part is the issue, if it's a simple character string then the problem regarding character arrays vis a vis cellstrings is also part of the problem.
I prefer to try to solve the problem if I believe I can infer what it is rather than wait...asking for clarification is ok and if made the wrong assumption(s) then it'll turn out to have been necessary and can fixup previous assumptions as needed, if needed...
If it turns out as something else (or in addition too), OP can amplify...meanwhile, if it is correct he's already got his answer.
this is my code ,
k=2; % purely because I have a specific item to go into first cell
title= repmat('a', [84 1]); % this may be where my error relates
for j=1:965
% other code that is irrelevant for query is removed
aa = filename; %each data set has a term "filename" and is in the form of B01 etc.. I only put this line in to avoid having to write filename every time I want the term.
% the next line is what causes the error. I have tried above mentioned option and im still getting an error
title(k)= {aa};
k=k+1;
end
The error currently coming up is as follows: Conversion to char from cell is not possible.
its probably the repmat expression - obtained this from searches on the internet after (obviously) zeros(84,1) didnt work.
PS - FYI She not he :)
You define the variable title to be a character array using the command repmat('a',[84,1]). The {} is used exclusively for cell arrays and tables, but not for numeric or character arrays. You define a cell array with {aa}, and then try to place this inside a character array, which is not meaningful in MATLAB. So when you try to index into title like that, MATLAB gets confused...
Important note: do NOT use title as a variable name, as this is the name of an inbuilt function use to set figure (axes) titles . Similarly you should avoid using i, j, log, size, length...
title= repmat('a', [84 1]);
...
aa = filename;
% the next line is what causes the error. ...
title(k)= {aa};
Having defined title as a character array, now when you try to reference a single value, as I mentioned in first response that corresponds to a single character (in this case one 'a').
To quote Dolly from a visit on the Carson Tonight show years ago, "You can't put 10 pounds of 'taters in a 5-lb tote!" I'll leave you to infer what the reference was to... :)
In this case you're trying to either put multiple characters into a location with only room for one if you use the form without the curlies or, as the actual error here, you can't mix cell arrays and ordinary arrays indiscriminately.
Not sure what the point of the title array is; if you're trying to build an array of strings for use later, then build that array from scratch as I noted as either strings or cellstrings.
It looks kinda' like the existing usage came from something else and has been selected on the basis of trial and error rather than understanding what was after.
As Stephen says, avoid title as an array; you alias the TMW-supplied function of the same name which is too useful in its own right to want to do that.

Sign in to comment.

Asked:

on 4 Jan 2015

Commented:

dpb
on 4 Jan 2015

Community Treasure Hunt

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

Start Hunting!