Why is 'print' creating a csv file?

2 Ansichten (letzte 30 Tage)
Ted Baker
Ted Baker am 26 Feb. 2020
Kommentiert: Star Strider am 26 Feb. 2020
I'm trying to save a plot as a high dpi png, however, matlab keeps svaing the output as a csv. Why is this? My code is as follows:
% Plots spectrum from E4407B Spectrum Analyser
filetoopen = '400kbps_10db_0,096k_spectrum.CSV';
% Workings
close all;
datafromfile = csvread(filetoopen, 15, 0);
freq = datafromfile(:,1);
power = datafromfile(:,2);
axisMHz = freq .* 0.000001;
filenamecomma = regexprep(filetoopen, '_', ' ');
filename = regexprep(filenamecomma, ',', '.');
expression = '(^|[\. ])\s*.';
replace = '${upper($0)}';
filename = regexprep(filename,expression,replace);
filename = newStr(1:end-4);
figure(1);
plot(axisMHz, power);
xlabel('Frequency (MHz)');
ylabel('Power (dBm)');
title(newStr);
print(gcf, filenamecomma, '-dpng', '-r300');
I've also included an example data file. Thanks in advance.

Akzeptierte Antwort

Star Strider
Star Strider am 26 Feb. 2020
Perhaps because you’re telling it to?
Running these lines:
filetoopen = '400kbps_10db_0,096k_spectrum.CSV';
filenamecomma = regexprep(filetoopen, '_', ' ')
produces:
filenamecomma =
'400kbps 10db 0,096k spectrum.CSV'
which is what print is saving to:
print(gcf, filenamecomma, '-dpng', '-r300');
Mystery solved!
  2 Kommentare
Ted Baker
Ted Baker am 26 Feb. 2020
Thanks for that - I forgot the .csv in the filename... I got it working by adding:
% remove .csv file extension
filenamecommatosave = filenamecomma(1:end-4);
and changing my print to:
print(gcf, filenamecommatosave, '-dpng', '-r300');
Thanks again.
Star Strider
Star Strider am 26 Feb. 2020
As always, my pleasure!
This is likely easier and more robust:
filetoopen = '400kbps_10db_0,096k_spectrum.CSV';
[~,filenamecommatosave,~] = fileparts(filetoopen);
producing:
filenamecommatosave =
'400kbps_10db_0,096k_spectrum'

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Environment and Settings finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by