How do I save a SINGLE value from the workspace?
조회 수: 3 (최근 30일)
이전 댓글 표시
Hello, I am using the diff function to find the value of 0 of my function in the first derivative. Turns out I got the results shown in the image.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1302310/image.png)
Now I need to know how I can save ONLY the value that is marked in a box of the image. Since later I need to perform a for and save the marked value for each iteration. Thank you.
댓글 수: 2
Luca Ferro
2023년 2월 21일
could you share the data? if not, could you say what data type (array 24x2, struct,...) is the source?
답변 (2개)
Fangjun Jiang
2023년 2월 21일
You will have to assign that value to a temparary variable and then save it.
a=rand(10,2);
%save test.mat a(8,:) % this will cause error
temp=a(8,:);
save test.mat temp;
댓글 수: 0
Star Strider
2023년 2월 21일
‘Hi, is a data type 2x99. I need save the value x(:,1) when x(:,2) is near to 0. How I save this date.’
It depends what ‘near to 0’ means. One option is to look for the indices of the zero crossings —
x = [0:98; sin(2*pi*(0:98)/25)+randn(1,99)*0.05].' % Create Data
Near0Idx = find(diff(sign(x(:,2)))) % Indices Of Zero Crossings
figure
plot(x(:,1), x(:,2), 'DisplayName','Data')
hold on
plot(x(Near0Idx,1), x(Near0Idx,2), 'rs', 'DisplayName','Near to 0')
hold off
grid
axis('padded')
legend('Location','best')
If there are no zero-crossings, then equivalently something like this would work:
Offset = 1;
Near0Idx = find(diff(sign(x(:,2)-Offset)))
The ‘Offset’ value can be whatever works in your application, either positive or negative, and it could be either added or subtracted from ‘x(:,2)’.
.
.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Structures에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!