필터 지우기
필터 지우기

How do I save a SINGLE value from the workspace?

조회 수: 3 (최근 30일)
Camilo Mahnert Cataldo
Camilo Mahnert Cataldo 2023년 2월 21일
답변: Star Strider 2023년 2월 21일
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.
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
Luca Ferro 2023년 2월 21일
could you share the data? if not, could you say what data type (array 24x2, struct,...) is the source?
Camilo Mahnert Cataldo
Camilo Mahnert Cataldo 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.

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

답변 (2개)

Fangjun Jiang
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;

Star Strider
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
x = 99×2
0 -0.0154 1.0000 0.2117 2.0000 0.4525 3.0000 0.7344 4.0000 0.8145 5.0000 0.8883 6.0000 1.0496 7.0000 0.8734 8.0000 0.8215 9.0000 0.7359
Near0Idx = find(diff(sign(x(:,2)))) % Indices Of Zero Crossings
Near0Idx = 8×1
1 13 25 38 51 63 75 88
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)’.
.
.

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by