Azzera filtri
Azzera filtri

How to name and save multiple variable files in .png format.

1 visualizzazione (ultimi 30 giorni)
I have the following code, which imports data from a .txt file and converts it into variables. Currently, I am naming these variables based on angles P and N and visualizing them using the 'image' command. If I wish to save all of this information, what steps should I take?
function required for the below code are attached with the question.
.txt files are too large to attach, please use any of your chice to show me this use case.
Any help is much appreciated, regards
clc;
clear;
close all;
% Import the data
AngleN2N4 = importfile();
AngleP2N4 = importfile();
AngleP2P4 = importfile();
AngleN2P4 = importfile();
TAngle42 = AngleN2P4 + AngleP2P4 + AngleN2N4 + AngleP2N4;
figure(1)
image1 = image(TAngle42,'CDataMapping','scaled');
title('X = +/-2 and Y = +/-4');
colorbar;
% colormap("winter")
AngleN2N5 = importfile();
AngleP2N5 = importfile();
AngleP2P5 = importfile();
AngleN2P5 = importfile();
TAngle52 = AngleN2P5 + AngleP2P5 + AngleN2N5 + AngleP2N5;
figure(2)
image2 = image(TAngle52,'CDataMapping','scaled');
title('X = +/-2 and Y = +/-5');
colorbar;
diff = TAngle52 - TAngle42;
figure(3)
image3 = image(diff,'CDataMapping','scaled');
for k = 1:5
for m = 2
for n = [4,5]
figure(k)
image('AngleN%dP%d',m, n,'CDataMapping','scaled')
title('X = +/-m and Y = +/-n');
colorbar;
image('AngleP%dN%d',m, n,'CDataMapping','scaled')
title('X = +/-m and Y = +/-n');
colorbar;
end
end
end
  1 Commento
Stephen23
Stephen23 il 23 Gen 2024
Modificato: Stephen23 il 23 Gen 2024
"Currently, I am naming these variables based on angles P and N ..."
And that is the problem right there.
"If I wish to save all of this information, what steps should I take?"
Do not use meta-data in variable names (unless you want to force yourself into writing slow, complex, inefficient, obfuscated code).
Learn to use arrays and indexing. Then your task is easy.

Accedi per commentare.

Risposta accettata

Hassaan
Hassaan il 23 Gen 2024
1. Save as MAT-file
MAT-files are useful for storing MATLAB variables and can be easily loaded back into MATLAB later. To save your data:
save('myData.mat', 'AngleN2N4', 'AngleP2N4', 'TAngle42', 'AngleN2N5', 'AngleP2N5', 'TAngle52', 'diff');
2. Save as CSV
If you want to export your data to a CSV file, which can be opened in other software like Excel, use the writetable function:
writetable(array2table(TAngle42), 'TAngle42.csv');
writetable(array2table(TAngle52), 'TAngle52.csv');
writetable(array2table(diff), 'diff.csv');
3. Save Figures/Images
To save the figures generated by the image command:
% For the first figure
figure(1);
image1 = image(TAngle42,'CDataMapping','scaled');
title('X = +/-2 and Y = +/-4');
colorbar;
saveas(gcf, 'Figure1.png');
% For the second figure
figure(2);
image2 = image(TAngle52,'CDataMapping','scaled');
title('X = +/-2 and Y = +/-5');
colorbar;
saveas(gcf, 'Figure2.png');
% For the third figure
figure(3);
image3 = image(diff,'CDataMapping','scaled');
title('Difference between TAngle52 and TAngle42');
colorbar;
saveas(gcf, 'Figure3.png');
These commands save the images as PNG files, but you can also save them in other formats like JPG or PDF by changing the file extension in the saveas function.Additional Notes
  • Ensure you have the required permissions to write files to your current directory.
  • Adjust the paths and filenames as needed for your specific use case.
  • The CSV saving method assumes your data can be converted to a table format (array2table). If the data is not compatible with this format, consider other saving methods or reshape the data accordingly.
Remember to run these saving commands at the appropriate places in your script where the data is available and before clearing or closing the figures.
-----------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
It's important to note that the advice and code are based on limited information and meant for educational purposes. Users should verify and adapt the code to their specific needs, ensuring compatibility and adherence to ethical standards.
Professional Interests
  • Technical Services and Consulting
  • Embedded Systems | Firmware Developement | Simulations
  • Electrical and Electronics Engineering
Feel free to contact me.
  5 Commenti
Stephen23
Stephen23 il 23 Gen 2024
"But I still need to know how can I save these mat files with distinctive titles stating their respective angles?"
Generate the title using e.g. SPRINTF or COMPOSE. Also for filenames:

Accedi per commentare.

Più risposte (0)

Prodotti


Release

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by