How to define a path in saveas command?

Hey Matlab users,
I've got a problem in defining a path for saveas command. When i want to save my Figure, the file is directly been saved in a path that is already on top of the command window in front of the "Current Folder". I tried to change the address by fname:
fname = 'D:\path1\path2';
saveas(gca,filename,'jpeg');
But it didn't work!!!
I very much appreciate your hint :) Mehdi

 Akzeptierte Antwort

Jan
Jan am 6 Okt. 2011

26 Stimmen

You have to consider the variable fname in the call of saveas:
fname = 'D:\path1\path2';
saveas(gca, fullfile(fname, filename), 'jpeg');
I assume "fpath" would be more meaningful than "fname".

7 Kommentare

Andy
Andy am 19 Dez. 2017
thank you very much I lost hours on my problem until you
Inorder to save file on to the same directory where my matlab program is there, I used to use this command:
print('-dpng',['FirstVar',num2str(A),'SecondVariable',num2str(B),'ForLoopIndex',num2str(i),'.png'])
How can I modify your code to accomplish this?
To accomplish what? Where is your "matlab program"? Do you mean matlab.exe or an M-file? Is it a compiled application?
I find this version easier to read:
FiileName = sprintf('FirstVar%dSecondVariable%dForLoopIndex%d.png', A, B, i);
File = fullfile(Folder, FileName)
print('-dpng', File);
I guess, that you want to file to be stored in the folder of the M-file. Then:
Folder = fileparts(mfilename('fullpath'));
@Jan, extremely sorry for posing the problem in a bad way.
I have a matlab code located at “D:\FolderA” which is used to analyze data located at “E:\DataFiles”. Matlab code will generate some plot, which I want to save in “E:\DataFiles”. If I wanted to save the file in the same folder where my program is, the I was using :
print('-dpng',['FirstVar',num2str(A),'SecondVariable',num2str(B),'ForLoopIndex',num2str(i),'.png'])
My question was what if I want to save it to “E:\DataFiles”.
Your solution helped me. Thank you
Marcus Tan
Marcus Tan am 29 Aug. 2021
Bearbeitet: Jan am 30 Aug. 2021
I need help to save an stft fig file format to .bmp file format. Can someone help me pls..im stuck for multiple weeks. this is my code
clear all; clc
%%read file by selecting the files
[file_list, path_n] = uigetfile('21.eea', 'Grab the files you process', 'Multiselect', 'on');
tic
if iscell(file_list) == 0
file_list = {file_list};
end
%%processing file by adjusting the samples being used
for i=1:length(file_list)
filename = file_list{i};
data_in = importdata([path_n filename]);
fs=128; %sampling frequency
ts=1/fs; %Sampling interval
x=ts*(1:size(data_in(1:7000),1));
B=data_in(1:1750);%Channel 1 of EEG
C=data_in(1751:3500);%Channel 1 of EEG
D=data_in(3501:5250);%Channel 1 of EEG
E=data_in(5251:7000);%Channel 1 of EEG
A1=B+C+D+E;
B1=data_in(7001:8750);%Channel 2 of EEG
C1=data_in(8751:10500);%Channel 2 of EEG
D1=data_in(10501:12250);%Channel 2 of EEG
E1=data_in(12251:14000);%Channel 2 of EEG
A2=B1+C1+D1+E1;
B2=data_in(14001:15750);%Channel 3 of EEG
C2=data_in(15751:17500);%Channel 3 of EEG
D2=data_in(17501:19250);%Channel 3 of EEG
E2=data_in(19251:21000);%Channel 3 of EEG
A3=B2+C2+D2+E2;
B3=data_in(21001:22750);%Channel 4 of EEG
C3=data_in(22751:24500);%Channel 4 of EEG
D3=data_in(24501:26250);%Channel 4 of EEG
E3=data_in(26251:28000);%Channel 4 of EEG
A4=B3+C3+D3+E3;
B4=data_in(28001:29750);%Channel 5 of EEG
C4=data_in(29751:31500);%Channel 5 of EEG
D4=data_in(31501:33250);%Channel 5 of EEG
E4=data_in(33251:35000);%Channel 5 of EEG
A5=B4+C4+D4+E4;
B5=data_in(35001:36750);%Channel 6 of EEG
C5=data_in(36751:38500);%Channel 6 of EEG
D5=data_in(38501:40250);%Channel 6 of EEG
E5=data_in(40251:42000);%Channel 6 of EEG
A6=B5+C5+D5+E5;
B6=data_in(42001:43750);%Channel 7 of EEG
C6=data_in(43751:45500);%Channel 7 of EEG
D6=data_in(45501:47250);%Channel 7 of EEG
E6=data_in(47251:49000);%Channel 7 of EEG
A7=B6+C6+D6+E6;
B7=data_in(49001:50750);%Channel 8 of EEG
C7=data_in(50751:52500);%Channel 8 of EEG
D7=data_in(52501:54250);%Channel 8 of EEG
E7=data_in(54251:56000);%Channel 8 of EEG
A8=B7+C7+D7+E7;
B8=data_in(56001:57750);%Channel 9 of EEG
C8=data_in(57751:59500);%Channel 9 of EEG
D8=data_in(59501:61250);%Channel 9 of EEG
E8=data_in(61251:63000);%Channel 9 of EEG
A9=B8+C8+D8+E8;
B9=data_in(63001:64750);%Channel 10 of EEG
C9=data_in(64751:66500);%Channel 10 of EEG
D9=data_in(66501:68250);%Channel 10 of EEG
E9=data_in(68251:70000);%Channel 10 of EEG
A10=B9+C9+D9+E9;
B10=data_in(70001:71750);%Channel 11 of EEG
C10=data_in(71751:73500);%Channel 11 of EEG
D10=data_in(73501:75250);%Channel 11 of EEG
E10=data_in(75251:77000);%Channel 11 of EEG
A11=B10+C10+D10+E10;
B11=data_in(77001:78750);%Channel 12 of EEG
C11=data_in(78751:80500);%Channel 12 of EEG
D11=data_in(80501:82250);%Channel 12 of EEG
E11=data_in(82251:84000);%Channel 12 of EEG
A12=B11+C11+D11+E11;
B12=data_in(84001:85750);%Channel 13 of EEG
C12=data_in(85751:87500);%Channel 13 of EEG
D12=data_in(87501:89250);%Channel 13 of EEG
E12=data_in(89251:91000);%Channel 13 of EEG
A13=B12+C12+D12+E12;
B13=data_in(91001:92750);%Channel 14 of EEG
C13=data_in(92751:94500);%Channel 14 of EEG
D13=data_in(94501:96250);%Channel 14 of EEG
E13=data_in(96251:98000);%Channel 14 of EEG
A14=B13+C13+D13+E13;
B14=data_in(98001:99750);%Channel 15 of EEG
C14=data_in(99751:101500);%Channel 15 of EEG
D14=data_in(101501:103250);%Channel 15 of EEG
E14=data_in(103251:105000);%Channel 15 of EEG
A15=B14+C14+D14+E14;
B15=data_in(105001:106750);%Channel 16 of EEG
C15=data_in(106751:108500);%Channel 16 of EEG
D15=data_in(108501:110250);%Channel 16 of EEG
E15=data_in(110251:112000);%Channel 16 of EEG
A16=B15+C15+D15+E15;
p1= A1+A2+A3+A4+A5+A6+A7+A8+A9+A10+A11+A12+A13+A14+A15+A16;
%Signal Transformation using STFT and converting into grayscale
figure('Name','Schizophrenia Patient');
stft(p1,fs,'window',kaiser(256,5),'OverlapLength',220,'FFTLength',512);
colormap(flipud(gray));
stft_file=filename
toc
FolderName = ('C:\Users\Acer\Desktop\FYP 2021\Matlab (Proj)\Channels Split (STFT)\Schizophrenia (Combine Channels)'); % Your destination folder
saveas(gca, fullfile(FolderName, stft_file), '.bmp');
end
Marcus Tan
Marcus Tan am 29 Aug. 2021
Currently the saving is only saving .eea file. what i trying to do in this project is trying to plot the signals from .eea file format transform them to fig file format then i trying to save the fig file format to .bmp. Kindly advise. Thank you
...
for i=1:length(file_list)
filename = file_list{i}; % Now filename contains the .eea extension.
... % Your code
FolderName = 'C:\Users\Acer\Desktop\FYP 2021\Matlab (Proj)\Channels Split (STFT)\Schizophrenia (Combine Channels)';
[~, file] = fileparts(filename); % Remove extension
saveas(gca, fullfile(FolderName, [file, '.bmp']); % Append .bmp
end

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Wayne King
Wayne King am 6 Okt. 2011

0 Stimmen

You can either change directory to the directory you want to save it in, or you can specify the directory
plot(randn(100,1));
save(gca,'c:\figures\test','jpeg');

2 Kommentare

Wayne King
Wayne King am 6 Okt. 2011
Also if you want to define your figures directory
figuresdir = 'D:\figures\';
saveas(gcf,strcat(figuresdir, 'filename'), 'jpeg');
Instead of 'strcat' it is better to use the 'fullfile' function because it will handle the file separator and will work in different operating systems (will place correctly '\' in Windows, '/' in Linux etc.)
saveas(gcf, fullfile(figuresdir, 'filename'), 'jpeg');

Melden Sie sich an, um zu kommentieren.

Kategorien

Tags

Gefragt:

M G
am 6 Okt. 2011

Kommentiert:

Jan
am 30 Aug. 2021

Community Treasure Hunt

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

Start Hunting!

Translated by