Opening a txt file with chosen directory location

조회 수: 16 (최근 30일)
Ross
Ross 2014년 10월 27일
댓글: Ross 2014년 10월 30일
Hi.
I have a code that works to read values from a formatted txt file that I'm producing with another code. The code works as long as I have the code in the same directory and choose the file using 'uigetfile'. However as I want to eventually run the code on multiple files in a directory, when I set a variable to choose the directory, the code gives this error:
Error using fopen
First input must be a file name of type char, or a file identifier of type double.* *
My x_filename variable comes out as a cell array which I belive is the main issue but having tried to convert and change it, I get different types of errors.
I know this is probably a very simple fix but I need some assistance to find it.
My code is something like this:
% Directories for Files
x_Directory = uigetdir;
y_Directory = uigetdir;
x_Files = dir([x_Directory, '\*.txt']); % Determine number of files in chosen directory
x_Filename = {x_Files.name}'; % Sanity check
file_x = fopen(x_Filename); % open raw txt file
data = textscan(file_x,'%s %s %s %s %s %s %s %s %s %s %s %s %s');
fclose(file_x); % close raw file
celldisp(data);
% MAIN CODE HERE
I am testing the above with only one file before I implement the for loop to read multiple files, but the above error is stopping me.
Can anyone help?

채택된 답변

Robert Cumming
Robert Cumming 2014년 10월 27일
편집: Robert Cumming 2014년 10월 27일
x_Filename
is a cell array, you need to pass the individual file into fopen:
file_x = fopen ( x_Filename{1} )
You should also look at
fullfile
filename = fullfile ( x_Directory, x_Filename{1} );
file_x = fopen ( filename )
for connecting paths and files
  댓글 수: 23
Robert Cumming
Robert Cumming 2014년 10월 30일
I agree with Stephen it looks like you should have:
Array = zeros(noFiles,1);
Ross
Ross 2014년 10월 30일
I realised that after I posted, fixed now.
Thanks for all your help guys!
:)

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Image Analyst
Image Analyst 2014년 10월 30일
Try this snippet to get the full filename of what file the user browsed to.
% Have user browse for a file, from a specified "starting folder."
% For convenience in browsing, set a starting folder from which to browse.
startingFolder = 'C:\Program Files\MATLAB'; % Whatever you want....could be pwd if you want.
if ~exist(startingFolder, 'dir')
% If that folder doesn't exist, just start in the current folder.
startingFolder = pwd;
end
% Get the name of the file that the user wants to use.
defaultFileName = fullfile(startingFolder, '*.*');
[baseFileName, folder] = uigetfile(defaultFileName, 'Select a file');
if baseFileName == 0
% User clicked the Cancel button.
return;
end
fullFileName = fullfile(folder, baseFileName)
fid = fopen(fullFileName, 'rt');
% etc. more code......

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by