URGENT!! There's the possibility to control textread input??

1 view (last 30 days)
Hi you!
I have my guide file with an edit text and two pushbuttons to browse and load a file. I have to read it by textread. It' a file formed by 31 columns, most of them read as String.
I'd like to control that the format of the file user can browse was the same of mine (for example: user file has to be formed by 31 coloumns). I have no headerlines, so I can't control columns titles according to my variables...
I post you my textread code (please don't mind name of variables):
[anno azienda istituto disciplina_dimissione prog_reparto ...
regime_ricovero provenienza drg degenza dh importo_reg data_dim ...
residenza eta sesso d_principale d_com1 d_com2 d_com3 d_com4...
int_prin int_sec1 int_sec2 int_sec3 int_sec4 mod_accesso...
mod_dimissione tipo_ricovero data_ingresso tipo_prescrittore ...
cod_nos]=textread(filename,'%s %s %s %s %s %s %s %s %d %d %d %s %s %d %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s','delimiter','\t');
I'd like to find a way to control at least the number of columns, or the maximum number of characters per-coloumn, something that shows me an error if the file is not compatible...
Any ideas for me??

Accepted Answer

Walter Roberson
Walter Roberson on 19 Jan 2012
fmt = repmat('%s', 1, 31);
[...] = textread(filename, fmt, 'delimiter', '\t');
To control the maximum number of characters per column, or to show an error if the file is not compatible, use
InputLines = textread(filename, '[^\n]', 'delimiter', '\n');
and then parse the strings in InputLines yourself. This would satisfy the literal requirement to use textread() but still give you full flexibility about how you interpret the input.
  2 Comments
Jethro
Jethro on 19 Jan 2012
Thanks Walter, I set my error on length(ftm)...
About the second code you wrote...
I thought about it and I think it's not so useful for me because if I add a new line with columns names, they will be correct any times I take a different file, even if my file is empty...
Am I wrong?? Maybe I didn't understand anything xD
Walter Roberson
Walter Roberson on 19 Jan 2012
You have been required to use textread(). The code I showed "cheats" on textread() to return everything in the file as pure text, uninterpreted other than broken up in to lines. Once you have the lines of text, you can do anything you want with them. If the file is empty, you will get empty variables.
With the exact input lines as strings on hand, you can do things like test to see if the first line has any characters that cannot be present in numbers; if so then it is a header line. You can count the tabs on each input line to determine whether the number of fields per line matches your expectations, and to determine whether any line has missing or extra fields. You can break apart the line using regexp(), convert sub-strings to numeric form, whatever is appropriate, not mired down by the fact that textread() itself cannot make those checks.

Sign in to comment.

More Answers (0)

Categories

Find more on Migrate GUIDE Apps 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!