이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
Passing one value at a time
조회 수: 3 (최근 30일)
이전 댓글 표시
Hi,
I need to pass one value at a time from matrix. For example, first value would be 0.0087 the loop execute and display that, then 0.0074 and so on...
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/847990/image.png)
Any help is appreciated. Thanks in advance.
답변 (1개)
KSSV
2021년 12월 30일
A = rand(5,1)
for i = 1:length(A)
A(i)
end
댓글 수: 14
KSSV
2021년 12월 30일
@Sugar Mummy commented: This is still giving dimensionality error because the other parameters (say B,C,D...) are a constant value that's why I need a code that take one constant value at a time.
Your help would be appreciated.
Sugar Mummy
2021년 12월 30일
It says..
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/848030/image.png)
As told earlier the other parameters have constant values but 'A' parameter need multiple values that should be executed ONE AT A TIME.
Walter Roberson
2021년 12월 30일
Simulink can receive individual values from MATLAB in at least two different ways:
- You can use set_param() to change the value of some parameter of some block. For example you could use set_param to change the resistance for an RLC circuit; OR
- You can change a variable in the workspace of the function being used to invoke sim() to run a model (or sometimes you change the base workspace.)
In the first case where you are using set_param() then the name of the parameter in the block has no connection to the name of the variable in the MATLAB workspace. You could set the parameter name 'blkgain' from the content of the MATLAB variable elephant_toes(K) and Simulink would not have to care that the name of the parameter and the name of the MATLAB variable are different.
In the second case where you are changing workspace values, then you have a potential problem: the workspace variable that is changed must match the variable name used inside the Simulink block (such as in a Math block, or an initialization mask). If your simulink block has A.*B+C and you want to iterate through different A values, then you would need to change the MATLAB variable A .
And that could be a problem if A is also the name of the MATLAB variable that also holds all of the values. You cannot somehow tell Simulink to use the "currently selected" value out of the matrix A and somehow tell MATLAB to "select" a particular location inside A for Simulink to focus on. That will not work.
Instead, you are going to have to rename the aggregate variable that has all of the values, so that it does not match the name used inside Simulink. So for example,
All_A = A;
for K = 1 : numel(All_A)
%MATLAB workspace variable |A| must be changed to the single value that
%Simulink is to work with this time
A = ALL_A(K);
sim(whatever);
end
A = ALL_A; %restore after
Sugar Mummy
2021년 12월 30일
Hi,
Thank you for the response @Walter Roberson. I am not using the variable 'A' it was just for the example.My variable name is 'IR'. Therefore, as per my understanding All_A = IR so what would be 'A' then? Otherwise it would be a undefined variable error.
Btw, I am working on Simulink where I am creating a variable on model workspace, also modifying the code in it to pass each value one at a time while other parameters are constant.
Thanks in advance.
Walter Roberson
2021년 12월 30일
All_IR = IR;
for K = 1 : numel(All_IR)
%MATLAB workspace variable |A| must be changed to the single value that
%Simulink is to work with this time
IR = ALL_IR(K);
sim(whatever);
end
IR = ALL_IR; %restore after
Sugar Mummy
2021년 12월 30일
@Walter Roberson I tried exactly this way as well but my problem is still giving the same error i-e- dimesionality issue!
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/848170/image.png)
As shown in the figure below the All_IR dimension is 5x1
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/848175/image.png)
But the IR variable (the main one) value is 1x1 which means that it is taking ONLY the last value of loop.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/848180/image.png)
Could you please guide what could be the reason.
Walter Roberson
2021년 12월 30일
No computer language can do what you want. The IR variable cannot be simultaneously only a single value (to avoid dimension problems) but also all of the values together.
In terms of the loop I showed before, you would typically record the output of the sim() call, and extract values from that, and store them at the MATLAB level; after the for K loop you would then have the overall results.
Sugar Mummy
2021년 12월 31일
@Walter Roberson Thank you so much . The sim() command worked for me.
Last thing I want to ask is that Scope is saving only the data of last value of IR and not the overall loop values data. Could you please help me in this.Thanks!
Walter Roberson
2021년 12월 31일
No, I do not know of any way to get a Simulink Scope block to save values from previous runs.
I recommend saving the outputs at the MATLAB level and plotting them at the MATLAB level.
All_IR = IR;
for K = 1 : numel(All_IR)
%MATLAB workspace variable |A| must be changed to the single value that
%Simulink is to work with this time
IR = ALL_IR(K);
simOut = sim(whatever);
output_wanted{K} = find(simOut, 'VariableYouWantTheValueOf');
end
IR = ALL_IR; %restore after
outputs_as_matrix = cell2mat(cellfun(@(C) C(:), output_wanted, 'uniform', 0));
plot(ALL_IR, outputs_as_matrix)
except that you might need to record another axis of values and use plot3()
Sugar Mummy
2022년 1월 2일
The command
output_wanted{K} = find(simOut, 'VariableYouWantTheValueOf');
is not working for me as I have multiple(12) variables attached to the scope.
Can you please help anything relatedly so I can store the whole Scope data in Simulink. Thanks!
Walter Roberson
2022년 1월 3일
Store the names of the variables in an array, and use a loop to call find(simOut, NAME) for each of the variables.
참고 항목
카테고리
Help Center 및 File Exchange에서 Simulink Functions에 대해 자세히 알아보기
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 (한국어)