This MATLAB function reads data from an open text file into column vector A and interprets values in the file according to the format specified by formatSpec. Feb 2, 2017 - If you would like to analyze data generated from other sources, you will most likely need to import the data from a text, csv, or xls file.
Active6 years, 11 months ago
I have a text file with the following format of information:
There are different 'Names' and I have to arrange each name into an array slot. I would then need another way to allocate the date associated with each row to that specific spot. So for example, the first Name1 may have array{0}, but I would also need to associate the 34, 24, 36, and 46 somehow. I would also need to distinguish the different names from each other. What is the best way to do this? a 2x2 array does not seem to be the solution.
What I have so far is something along the lines of this:
Courtesy: Read txt file in Matlab
Could someone please tell me the best way to arrange the information? Thanks, help is much appreciated.
Community♦
intlintl1,14366 gold badges2727 silver badges6060 bronze badges
2 Answers
Judging from the code, why don't you use
textscan
uses space and newline as delimiters by default. If you give newline as delimiter explicitly, you loose the space as delimiter, and the portability (Windows often uses rn
as a single newline, whereas Unix-derived OSes use n
). So, given your data, just leave it out. Then you jump through hoops to remove 10 headerlines, while
textscan
already has a nice baked-in option for that. So, those steps aren't needed. You proceed by splitting the stuff by a pass through regexp
with a space as delimiter, but since textscan
already splits on space, that's not needed either. So, using the three lines above, you'll get
Now, now to store the data more conveniently. I can think of two ways:
- Cell arrays
- Structures
For both, you'll have to find the unique names first:
Using cell arrays
This will give you a cell-array of the data sorted by name:
Now you can index into
results
as follows: Remember that Matlab is 1-based, so that
a(3)
indicates the 3rd element of a
, not the 4th. Breakdown of the command:
- The function
arrayfun
loops through the elements of the input array, applies a function to each element, and collects the results in either a regular array (if possible) or a cell-array (when impossible (error) and when given'uniformoutput', false
). It's a bit like aforeach
-construct. - Taking the input array equal to the unique
names
found in the first step, the trick is in the function to apply to each name. The function@(x) data(strcmp(tmp{1},x),:)
first finds the indices for the the given name intmp{1}
(array containing all names) usingstrcmp
. These indices are then used to indexdata = [tmp{2:end}]
, i.e., all the other arrays. - The results for each individual unique name is then stored in the cell-array
results
.
Using Structures
You can go one step further and use the cell-array
results
to have a more human-readable data structure. After applying all the previous steps, execute this: Now you can reference to your data by name:
The syntax
your_struct.('someString')
is called dynamic structure referencing. It references or creates a field in the structure your_struc
called someString
. Now, if
names{ii}
contains underscores you want to get rid of, then you can define or
for spaces. Then use
Kudos to these guys for that last one.
Rody OldenhuisRody Oldenhuis35.4k77 gold badges4040 silver badges9090 bronze badges
First, you should definitely read the data in using the method suggested by Rody (+1 for Rody for pointing it out), so I'm going to assume you got that far and have a variable called tmp like in Rody's code example.
Now, if I understand the problem correctly, you need to be able to distinguish each row of your example dataset from the other rows (using dates?) but at the same time you also need to easily distinguish the different names, some of which will be the same across several rows (again, I'm getting this from your example dataset).
One possible way of approaching this (that does admittedly have one drawback) is to use a structure. I'm going to assume you have obtained the variable tmp in Rody's answer and we'll go from there. Use the code:
This code will create a structure where the first level within the structure has a field name for each unique name in the dataset (I've also included in the code the variable
NameVec
in the first level of the structure so it can be used to reference the various fields later with a loop). Then within each field (Name1, Name2, and Name3 in this example), I've saved a data matrix containing the data associated with that name (where the individual rows are preserved).The drawback to this approach is that if you want to get ALL the data back in one big array, you'll need to loop over the elements of the Struct.NameVec and retrieve the data matrix associated with each unique name. And loops are slow in matlab. So really, it does depend on how you plan to use the data.
Hope this helps!
ps, if you're not familiar with matlab structures, run this code:
and then run the code I provided above on
Colin T BowersColin T Bowerstmp
. Then have a look at the resulting structure called Struct
in the matlab variable editor. This should give you a feel for how they work. 11k66 gold badges4040 silver badges6666 bronze badges
Not the answer you're looking for? Browse other questions tagged arraysmatlabmultidimensional-arraytext-filesfopen or ask your own question.
Active1 year, 9 months ago
I have a CSV file, I want to read this file and do some pre-calculations on each row to see for example that row is useful for me or not and if yes I save it to a new CSV file. can someone give me an example?in more details this is how my data looks like: (string,float,float) the numbers are coordinates.
basically i want to save the rows that have for distances more than 50 or 50 in a new file.the string field should also be copied.thanks
Amro117k2121 gold badges218218 silver badges405405 bronze badges
HosseinHossein13.7k4949 gold badges116116 silver badges162162 bronze badges
5 Answers
You could actually use
xlsread
to accomplish this. After first placing your sample data above in a file 'input_file.csv'
, here is an example for how you can get the numeric values, text values, and the raw data in the file from the three outputs from xlsread
:You can then perform whatever processing you need to on the numeric data, then resave a subset of the rows of data to a new file using
xlswrite
. Here's an example:![Read in txt file matlab free Read in txt file matlab free](/uploads/1/2/5/6/125642547/958798381.jpg)
![Read In Txt File Matlab Read In Txt File Matlab](/uploads/1/2/5/6/125642547/548213916.jpg)
120k1313 gold badges238238 silver badges342342 bronze badges
If you really want to process your file line by line, a solution might be to use
fgetl
:- Open the data file with
fopen
- Read the next line into a character array using
fgetl
- Retreive the data you need using
sscanf
on the character array you just read - Perform any relevant test
- Output what you want to another file
- Back to point 2 if you haven't reached the end of your file.
Unlike the previous answer, this is not very much in the style of Matlab but it might be more efficient on very large files.
Hope this will help.
AdrienAdrien
You cannot read text strings with csvread.Here is another solution:
Steve Pitchers5,41455 gold badges3333 silver badges3737 bronze badges
yukyuk16.8k99 gold badges5858 silver badges9191 bronze badges
Just read it in to MATLAB in one block
You can then process it using logical addressing
ind50 is then an index of the rows where column 2 is greater than 50. So
will list all the strings for the rows of interest.Then just use
fprintf
to write out your data to the new fileAdrianAdrian
here is the doc to read a csv : http://www.mathworks.com/access/helpdesk/help/techdoc/ref/csvread.htmland to write : http://www.mathworks.com/access/helpdesk/help/techdoc/ref/csvwrite.html
EDIT
An example that works :
file.csv :
the code :
and the output file result :
This isn't probably the best solution but it works! We can read the CSV file, control the distance of each row and save it in a new file.
Hope it will help!
MichaëlMichaël3,04577 gold badges3434 silver badges5858 bronze badges