How can I open a text file, and then read in the headers of that text file using textread? (Errors 166 and 67)

1 view (last 30 days)
I'm trying to write a script that allows the user to select a specific text file. Once the user selects the file, the script then opens it, reads it, and reads in the headers of the columns of the text file using textread, storing the headers. I'm having problems opening the file. The file returns -1, which means it wasn't opened, which is resulting in errors in textread. Here is the code I have been using below, along with error messages. I would appreciate help on this. Thank you.
[fileName1 pathName1]= uigetfile('~/*_gyro_out_Z.txt','Select the appropriate test file under *_gyro_out_Z.txt');
filepath=fullfile('pathName1','fileName1');
fid = fopen(filepath);
[TimeHdr DriftELHdr DriftXELHdr DriftLOSHdr] = textread('fid', '%s %f %f %f');
DriftEL = char(DriftELHdr);
DriftXEL = char(DriftXELHdr);
DriftLOS = char(DriftLOSHdr);
Error messages include:
Error using textread (line 166)
File not found.
Error in multi_panel_plot_modified (line 67)
[TimeHdr DriftELHdr DriftXELHdr DriftLOSHdr] = textread('fid', '%s %f %f %f');
fid also returns -1, which indicates it wasn't opened.

Accepted Answer

dpb
dpb on 17 Jul 2013
[fileName1 pathName1]= uigetfile('~/*_gyro_out_Z.txt',... Z.txt');
filepath=fullfile('pathName1','fileName1');
The second above combines the two strings 'pathName1','fileName1' to create a filepath variable which contains the string 'pathName1\fileName1'
You want/intended
filepath=fullfile(pathName1,fileName1); % use the variable name instead
  2 Comments
Sarah
Sarah on 17 Jul 2013
Thank you for your help! After a few other adjustments, it worked!
[fileName1 pathName1]= uigetfile('~/*_gyro_out_Z.txt','Select the appropriate test file under *_gyro_out_Z.txt');
filepath=fullfile(pathName1,fileName1);
[TimeHdr DriftELHdr DriftXELHdr DriftLOSHdr] = textread(filepath, repmat('%s ', 1,4),1);
DriftEL = char(DriftELHdr);
DriftXEL = char(DriftXELHdr);
DriftLOS = char(DriftLOSHdr);
[Timecell D1 D2 D3]=textread(filepath, '%s %f %f %f', 'headerlines', 1);
dpb
dpb on 17 Jul 2013
Glad to help...
Debugging hint--when you get some symptom such as this in the future where it just seems it really, really should work, look at the inputs to the function at the command line or in debug or remove a few well-chosen trailing ;'s to see intermediary results.
In this case if you had done that on the line to see what filepath was being passed to the textread() function you would have seen the problem.
BTW, it zoomed over my head first go 'round of your using textread() here--it has the nicety of returning a set of "ordinary" arrays for numeric values but at the cost of having to open the file twice in your case to get the header then the data. In this case being as it's just that one header line it likely isn't noticeable but textscan() is able to make multiple calls into a file w/ a file handle eliminating the extra (hidden but still there) fopen/fclose operation on the file owing to textread always beginning from the beginning of the file.
If you (like me) think the loss of functionality of textread by it having become deprecated and on the list for future extinction, please send request to keep the functionality going forward to the official Matlab support at mathworks.com Only by making the case is there any chance at all albeit it's probably a slim one at best. :(

Sign in to comment.

More Answers (0)

Categories

Find more on Data Import and Export in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!