How do I save a SINGLE value from the workspace?

3 views (last 30 days)
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 Comments
Luca Ferro
Luca Ferro on 21 Feb 2023
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 on 21 Feb 2023
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.

Sign in to comment.

Answers (2)

Fangjun Jiang
Fangjun Jiang on 21 Feb 2023
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 on 21 Feb 2023
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)’.
.
.

Categories

Find more on Structures in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!