Use of dialogue boxes: interactively choosing directory and file to read at the beginning, and "save as" at the end, during the execution of script.

3 次查看(过去 30 天)
I have script that works fine, however since it has not to run only on my pc, I would like to have a script that does not have a fixed directory for looking for txt files at the beginning and one for saving at the end. I would like to have the possibility to choose the text file I have to read at the beginning of the script, and decide where and under what name I want to save the output file when I export my final variable into txt file in the end. I would like to do it interactively using simple dialogue box during the execution of the script. Can anyone help with that? Below is my script.
dir_files='C:\Users\IC5\Desktop\Matlab task';
dir_save='C:\Users\IC5\Desktop\Matlab task\save';
lista=dir(fullfile(dir_files, '*.txt'));
nuova_variabile=[];
nuova_variabile2=[];
for i=1:length(lista)
filename=lista(i).name;
fid=fopen(fullfile(dir_files,filename));
dati=textscan(fid, '%s');
data=dati{1,1};
for j=1:length(data);
if strncmp('$SDDBT',data(j,1), 6)==1;
nuova_variabile=[nuova_variabile; (data(j,1))];
elseif strncmp('$GPGLL',data(j,1), 6)==1;
nuova_variabile=[nuova_variabile; (data(j,1))];
end
end
end
b=char(nuova_variabile);
c=cellstr(b(:,1:6));
idx=strcmp(c,'$SDDBT')';
ii=[1 diff(idx)];
out=nuova_variabile(find(ii~=0));
% then add
ne=ceil(numel(out)/2);
out=cellfun(@(x,y) [x ' ' y],out(1:2:end), out(2:2:end),'un',0);
expression = ' ';
replace = ',';
out2= regexprep(out,expression,replace);
path_file=fullfile('C:\Users\IC5\Desktop\Matlab task\save','output2.txt');
fid=fopen(path_file,'wt');
x=out2;
[rows,cols]=size(x);
for i=1:rows
fprintf(fid,'%s,',x{i,:})
fprintf(fid,'%s\n',x{i,end})
end
fclose(fid);
Thanks in advance
Manuela

采纳的回答

Tom
Tom 2013-6-24
see the UIGETFILE and UIPUTFILE functions.
doc uigetfile
doc uiputfile

更多回答(2 个)

mb1400
mb1400 2013-6-24
Yes I did, but it give me an error during the execution of the script and I did not work out what I was doing wrong. I will try again and will come back with the error. Thanks for your answer.

mb1400
mb1400 2013-6-24
Thank you for the advice. I solved it, it was actually a quite easy thing, that how I did it:
[stmfile, stmpath] = uigetfile('*.txt', 'pick text file');
data = importdata(fullfile(stmpath, stmfile), '%s');
% for the import
[filename, pathname] = uiputfile('*.txt','Save file name');
path_file=fullfile(pathname,filename);
fid=fopen(path_file,'wt');
%for the export
=D

类别

Help CenterFile Exchange 中查找有关 Environment and Settings 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by