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.

조회 수: 1 (최근 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