이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
How to store a string data into an array
조회 수: 3 (최근 30일)
이전 댓글 표시
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.
답변 (1개)
madhan ravi
2018년 12월 9일
trainLabels = cell(315,1);
trainLabels(:)={'bass'}
댓글 수: 15
Janrex Pensader
2018년 12월 9일
the fold variable is changing every time because it is in a loop and i need to store those into the array.
madhan ravi
2018년 12월 9일
ah then it would be
trainLabels{i} = {your variable} % i is the loop iterator
Janrex Pensader
2018년 12월 9일
It's showing this error now
Unable to perform assignment because brace indexing is not supported for variables of this type.
madhan ravi
2018년 12월 9일
Ah upload your datas as a .mat file and upload the part of the code your trying to store datas
Janrex Pensader
2018년 12월 9일
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
madhan ravi
2018년 12월 9일
You didn't look into my answer properly! You forgot to add this line before the loop
trainLabels = cell(315,1);
Janrex Pensader
2018년 12월 9일
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}...
madhan ravi
2018년 12월 9일
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.
Janrex Pensader
2018년 12월 9일
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.
madhan ravi
2018년 12월 9일
편집: madhan ravi
2018년 12월 9일
"I'm going to connect this to a code that needs an ordinary array of names."
why not ? you can easily connect it
Janrex Pensader
2018년 12월 9일
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?
madhan ravi
2018년 12월 9일
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?
Janrex Pensader
2018년 12월 9일
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
참고 항목
카테고리
Help Center 및 File Exchange에서 File Operations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
아시아 태평양
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)
