how can we construct different cells inside cell-array?
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
hi there,
I have a cell array of 1x6 size; 6 cell in one rows which contains a data set. Now, I want to construct a 7th cell in which I wanted to store some information regarding experiment. For that, I need to make different cell inside the 7th cell. for instance:
cell_array {1,7} ={ {Date :}, {Name :}, {Exp_time :}, {Remarks : }}
Your help will be greatly appreciated.
Best,
Sushil
채택된 답변
Voss
2022년 6월 26일
Perhaps a structure would be useful for storing that information:
exp_info = struct( ...
'Date','2022.06.29', ...
'Name','experiment_1', ...
'Exp_time','00:05:00', ...
'Remarks','Wednesday morning at five o''clock, as the day begins');
Store it in the 7th cell of cell_array, like you plan to:
cell_array = repmat({rand(10)},1,6);
cell_array{1,7} = exp_info
cell_array = 1×7 cell array
{10×10 double} {10×10 double} {10×10 double} {10×10 double} {10×10 double} {10×10 double} {1×1 struct}
cell_array{1,7}
ans = struct with fields:
Date: '2022.06.29'
Name: 'experiment_1'
Exp_time: '00:05:00'
Remarks: 'Wednesday morning at five o'clock, as the day begins'
Or rearrange the thing and store the experimental data in the same structure:
% - one possibility -
exp_info.Results = cell_array(1:6)
exp_info = struct with fields:
Date: '2022.06.29'
Name: 'experiment_1'
Exp_time: '00:05:00'
Remarks: 'Wednesday morning at five o'clock, as the day begins'
Results: {[10×10 double] [10×10 double] [10×10 double] [10×10 double] [10×10 double] [10×10 double]}
% - another possibility -
exp_info.Results = cell2struct(cell_array(1:6),sprintfc('Result_%d',1:6),2)
exp_info = struct with fields:
Date: '2022.06.29'
Name: 'experiment_1'
Exp_time: '00:05:00'
Remarks: 'Wednesday morning at five o'clock, as the day begins'
Results: [1×1 struct]
exp_info.Results
ans = struct with fields:
Result_1: [10×10 double]
Result_2: [10×10 double]
Result_3: [10×10 double]
Result_4: [10×10 double]
Result_5: [10×10 double]
Result_6: [10×10 double]
댓글 수: 6
hi @Voss this is what I exactly need but is it possible to make user input program because I have enter this information every time when I complete the experiment.
You can use input to gather user input:
Date = input('Enter the Date: ','s');
Name = input('Enter the Name: ','s');
% etc.
Or you can make a GUI:
hi @voss, sorry to bother you I have one more question;
I tried to make a struct_array I have written this code to store the information in such a way that every time I update the new file it supposed to add new field in the cell but instead of creating new field it is replacing the previous field. For instance previously if I have cell_blocknew{1,c}.file1, the new file is
cell_blocknew{1,c}.file2 But I wanted in cell_blocknew{1,c}->file1
cell_blocknew{1,c}->file2
and each time ccell will increment by 1. e.g 1 2 3 and so on.
Do you have any suggestion? your help will be greatly appreciated.
[~,c]=size(cell_blocknew);
[~,ccell]=size(cell_blocknew{1,1});
filename=sprintf('file%d',ccell);
cell_blocknew{1,c}.(filename)= struct( ...
'Flystrain',input('please specify the fly strain: ','s'), ...
'Date',input('mm:dd:yyyy ','s'), ...
'Expt_starttime',input('hh:mm:ss ','s'), ...
'Position_deadflies',input('Enter the position of dead flies: '), ...
'Remarks',input('Note: ','s'));
It's not completely clear to me what you want to do or what the problem is, but instead of having numbered fields in a structure (file1, file2, etc.), maybe use a structure array (which you say you tried, so maybe this is your intent anyway):
% initialize empty struct array with the right fields:
S = struct( ...
'Flystrain',{}, ...
'Date',{}, ...
'Expt_starttime',{}, ...
'Position_deadflies',{}, ...
'Remarks',{});
% add a new struct element to the struct array:
% (presumably this is inside a loop in the actual code)
S(end+1) = struct( ...
'Flystrain',input('please specify the fly strain: ','s'), ...
'Date',input('mm:dd:yyyy ','s'), ...
'Expt_starttime',input('hh:mm:ss ','s'), ...
'Position_deadflies',input('Enter the position of dead flies: '), ...
'Remarks',input('Note: ','s'));
I call the structure array S because I'm not sure how it would fit in with your variables.
So, actually I have a cell array of 1x7: cell_array{1,1:6},cell_array{1,7}. What I am trying to do is I am trying struct_array inside cell_array{1,7} in order to store some information. and what I want is each time when I update the data in cell_array have to store separate information. So that I need struct_array inside the cell_array{1,7} should be increamented by 1.
for instance:
cell_array{1,7}(1)='Flystrain',input('please specify the fly strain: ','s'), ...
'Date',input('mm:dd:yyyy ','s'), ...
'Expt_starttime',input('hh:mm:ss ','s'), ...
'Position_deadflies',input('Enter the position of dead flies: '), ...
'Remarks',input('Note: ','s'));
similarly,
cell_array{1,7}(2)='Flystrain',input('please specify the fly strain: ','s'), ...
'Date',input('mm:dd:yyyy ','s'), ...
'Expt_starttime',input('hh:mm:ss ','s'), ...
'Position_deadflies',input('Enter the position of dead flies: '), ...
'Remarks',input('Note: ','s'));
but things are not working in my way. when I try to store
cell_array{1,7}(2)='Flystrain',input('please specify the fly strain: ','s'), ...
'Date',input('mm:dd:yyyy ','s'), ...
'Expt_starttime',input('hh:mm:ss ','s'), ...
'Position_deadflies',input('Enter the position of dead flies: '), ...
'Remarks',input('Note: ','s'));
it just replace cell_array{1,7}(1). Does it make sense to you? i have tried the recent one which you just send me. still its not working. So, sorry I am killing your time.
Dear voss, I really appreciate your help. thank you so much you are absolutely right. I made a slight mistake in my code. Thank you so once again for your time and your constant help.
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Structural Mechanics에 대해 자세히 알아보기
태그
참고 항목
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)
