How to store a string data into an array

I have a an array like this:
trainLabels = zeros(315,1);
And I would like to put a string for example 'bass' on every element of that array. I tried using
fold = 'bass';
testLabels(testctr,1) = fold;
but its giving me this error:
Unable to perform assignment because the size of the left side is 1-by-1 and the size of the right side is 1-by-4.

Answers (1)

trainLabels = cell(315,1);
trainLabels(:)={'bass'}

15 Comments

the fold variable is changing every time because it is in a loop and i need to store those into the array.
ah then it would be
trainLabels{i} = {your variable} % i is the loop iterator
It's showing this error now
Unable to perform assignment because brace indexing is not supported for variables of this type.
Ah upload your datas as a .mat file and upload the part of the code your trying to store datas
the data that I need to store are foldernames ang here is my code:
files = dir('C:\Program Files\MATLAB\R2018a\bin\vision\data\');
files(ismember( {files.name}, {'.', '..'})) = [];
dirFlags = [files.isdir];
% Extract only those that are directories.
subFolders = files(dirFlags);
% Print folder names to command window.
trainctr =1;
testctr =1;
trainLabels = zeros(315,1);
testLabels = zeros(336,1);
trainingImages = zeros(32,32,3, 315);%315
testingImages = zeros(32,32,3, 336);%336
%subfolder loop
for k = 1 : length(subFolders)
%FOR DEBUGGING: Print all subfolders
%fprintf('Sub folder #%d = %s\n', k, subFolders(k).nametest(:,:,1,1));
direc = 'C:\Program Files\MATLAB\R2018a\bin\vision\data\';
newDir = strcat(direc,subFolders(k).name);
subname = subFolders(k).name;
%FOR DEBUGGING: Print new Directory
%fprintf('New Dir: %s\n', newDir);
scanDir = dir(newDir);
scanDir(ismember( {scanDir.name}, {'.', '..'})) = [];
scanFlags = [scanDir.isdir];
scanSubFolders = scanDir(scanFlags);
%note category loop
for i = 1 : length(scanSubFolders)
newDir2 = strcat(newDir,'\');
newDir2 = strcat(newDir2,scanSubFolders(i).name);
newDir3 = newDir2;
newDir2 = strcat(newDir2,'\*.png');
imagefiles = dir(newDir2);
nfiles = length(imagefiles);
fprintf('%s',subFolders(k).name);
fprintf('/');
fprintf('%s\n',scanSubFolders(i).name);
fold = scanSubFolders(i).name;
%image loop
for ii=1:nfiles
fprintf('%s\n',imagefiles(ii).name);
%
photo=imagefiles(ii).name;
filename = fullfile(newDir3,photo);
Image = imread(filename);
folder = strcmp('testing',subname);
if folder == 1
test(:,:,:,testctr) = Image;
testLabels{testctr} = {fold};
testctr = testctr+1;
else
train(:,:,:,trainctr) = Image;
trainLabels{trainctr} = {fold};
trainctr = trainctr+1;
end
end
fprintf('\n');
end
fprintf('\n');
end
You didn't look into my answer properly! You forgot to add this line before the loop
trainLabels = cell(315,1);
owhh sorry can you explain what that line will do?
I added trainLabels = cell(315,1); at the top of the code and the error is gone but when i checked the elements of the array it shows this.
testLabels
testLabels =
336×1 cell array
{1×1 cell}
{1×1 cell}
{1×1 cell}
{1×1 cell}...
It's called preallocation it ensures that the variable is a cell array.
celldisp(testLabels) %to view the contents of the cell
If my answered solved your question make sure to accept the answer and give a vote.
are there any other ways to do this? because I'm going to connect this to a code that needs an ordinary array of names.
"I'm going to connect this to a code that needs an ordinary array of names."
why not ? you can easily connect it
The code I'm going to connect it to is the object detection sample code from mathworks.
My code will replace the loading of cifar data to the cnn. the trainLabels will replace the array trainingLabels, and testLabels will replace testingLabels. if i sue your solution for my probem will this code accept my array?
To be frank and honest I don't have any experience with that field but in the link you suggested cell arrays ahd been used , why not try it and find it out?
It didnt work it cause an error it shows "Invalid training data. Y must be a vector of categorical responses." where y is the trainingLabels

Sign in to comment.

Categories

Asked:

on 9 Dec 2018

Commented:

on 9 Dec 2018

Community Treasure Hunt

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

Start Hunting!