randsource = randi([1 size(images,1)]);% randomly sample 1 image store as randsource>> 1 = computer, 2 = human
sources = images(randsource).name; % get the corresponding name of the image
source=(imread(sources));
abc= str2double(convertCharsToStrings(sources)) % store the image under the conditions paramter
trialMatrix(t,7)= cellstr(abc)
Sources is an image, however I just need the name of it stored in the matrix, but I am aware that it needed to converted, just not sure how to convert it to a "double" as trialMatrix is a 1x 7 double for some reason, so that is the error I keep getting. Tried to convert num2cell but I have some characters that are stored.

 채택된 답변

Adam Danz
Adam Danz 2023년 1월 5일

0 개 추천

> Sources is an image, however I just need the name of it stored in the matrix
Assuming sources is a string or character vector, then imageNames or whatever variable used to store the image names should be a cell array
Pre-allocate imageNames using
imageNames = {};
% or
imageNames = cell(__,__); % if you know what size it should be
then,
imageNames(t,7) = cellstr(sources);
You can get rid of the str2double line. This was converting "sources" to numeric (!?).

댓글 수: 4

Im a little confused. When I did that it erased the variables stored in my trialMatrix. I tried to reassign with
imagesname(t,7)= cellstr(sources)
trialMatrix(t,7)=imagesname(t,7)
but it gave me
"Conversion to double from cell is not possible"
Adam Danz
Adam Danz 2023년 1월 6일
The first block of code in my answer will clear everything from imageNames so that should be called before you assign anything to that variable.
Please show what us what these two variables contain. A screen shot or a copy-paste will due.
  • sources
  • imagesname
Jade T
Jade T 2023년 1월 6일
Thanks!
Adam Danz
Adam Danz 2023년 1월 6일
Thanks for the screenshots. The variable types are what I expected.
  1. The imageNames is probably erased because you're creating that cell array using my suggestions after you've aleady assigned data to it. imageNames = {}; --or-- imageNames = cell(__,__) should be called before any data are assigned to that variable.
  2. "Conversion to double from cell is not possible" - this is the error message that is expected if you're trying to assigning a cell to a numeric array.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Data Type Conversion에 대해 자세히 알아보기

질문:

2023년 1월 5일

댓글:

2023년 1월 6일

Community Treasure Hunt

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

Start Hunting!

Translated by