How to read file in matlab? - file

I have a txt file, and the content of the file is rows of numbers,
each row have 5 float number in it, with comma seperate between each number.
example:
1.1 , 12 , 1.42562, 3.5 , 2.2
2.1 , 3.3 , 3 , 3.333, 6.75
How can I read the file content into matrix in matlab?
So far I have this:
fid = fopen('file.txt');
comma = char(',');
A = fscanf(fid, ['%f', comma]);
fclose(fid);
The problem is that it's only give me the first line and when I
try to write the content of A I get this: 1.0e+004 * some number
Can someone help me please?
I guess that for the file I need to read it in a loop but I don't know how.
Edit: One more question: When I do output to A I get this:
A =
1.0e+004 *
4.8631 0 0 0 0.0001
4.8638 -0.0000 -0.0000 0.0004 0.0114
4.8647 -0.0000 -0.0000 0.0008 0.0109
I want the same values that in the file to be in the matrix, how can I make the numbers to be regular float and not formatted like this? Or are the numbers in the matrix actually float, but the output is just displayed like this?

MATLAB's built-in dlmread function would be a much easier solution for what you want to accomplish.
A = dlmread('filename.txt',',') % call dlmread and specify a comma as the delimiter

try with using importdata function
A = importdata(`filename.txt`);
It will solve your question.
EDIT
Alternative 1)
A = dlmread('test_so.txt',',');

The answer is surprisingly simple:
fid = fopen('depthMap.txt');
A = fscanf(fid, '%f');
fclose(fid);

Related

Getting back empty arrays/ variables when uploading .txt file to Matlab

I am trying to load a .txt file with data to Matlab to use for some calculations. However, when I run the code the variables/arrays come back empty or blank.
Below I have the code I am using.
%% importing the data
% Open file in the memory
fileID = fopen('rainfall.txt');
% Read the txt file with formats: Integer, Integer, Float
% Treat multiple delimiters, which is "space" in here, as one. Put the data
% in a variable called chunk.
chunk = textscan(fileID,'%d %d %f','Delimiter',' ',...
'MultipleDelimsAsOne',1);
% Close file from the memory.
fclose(fileID);
% date
dt = chunk{:,1};
% hour
hr = chunk{:,2};
% precip
r = chunk{:,3};
% remove extra variables from Matlab workspace
clear fileID ans
In the Workspace tab in Matlab it shows chunk to be an empty 1x3 cell. This results in dt, hr, and r not having any values either and are listed as having a value of []. So my best guess is that something is going wrong with loading in the data to Matlab.
Also, here is small portion of the data I am working with. This is exactly how it is written in the .txt file as well.
STATION DATE HPCP
----------------- -------------- --------
COOP:132367 20040116 22:00 0.01
COOP:132367 20040116 23:00 0.01
COOP:132367 20040117 00:00 0.04
COOP:132367 20040117 01:00 0.02
COOP:132367 20040117 02:00 0.00
In the actual file I have a lot more data than what I have listed here, but this should give an idea of what the data looks like and how it's formatted.
From the textscan help page:
textscan attempts to match the data in the file to the conversion specifier in formatSpec. The textscan function reapplies formatSpec throughout the entire file and stops when it cannot match formatSpec to the data.
So the first problem is the title lines. You should discard them. For example, by manually reading 2 lines (using fgetl).
Next, you should make sure that the format matches the data. You tried reading 2 integers and a float but you also have the station name.
I think the following should be ok:
fileID = fopen('rainfall.txt');
l = fgetl(fileID);
l = fgetl(fileID);
chunk = textscan(fileID,'%s:%d %d %d %f','Delimiter',' ',...
'MultipleDelimsAsOne',1);

Open file with formatted variable name in Julia

I have a list of files numbered gll_01.tab, gll_02.tab, ...., gll_20.tab in a subdirectory of my parent directory. These files are tabular data files.
I want to open/read files with user-specified input.
I can do:
a = 3
open("directory/gll_0$a.tab")
But using this approach, I would have to define two separate variable names for (01 to 09) and for (10 to 18). How can I use variables or strings with name 02, 03, ..., etc?
In python, I can have an equivalent command:
a = 4
g = '{:02d}'.format(a)
f = open('directory/gll_%s.tab' %g)
Is there an equivalent string formatting command in Julia?
A simple answer in this case would be to use lpad:
a = 3
open("directory/gll_$(lpad(a,2,"0")).tab")
If you need more fancy formatting you can use e.g. https://github.com/JuliaIO/Formatting.jl, in this case this would be:
using Formatting
a = 3
open("directory/gll_$(fmt("0>2", a)).tab")
Another option is to use #sprintf, docs are here. With that you can use %02d as a formatting option that would pad a digit d to length 2 with 0s preceding it:
julia> using Printf # this is in the standard library
julia> #sprintf("directory/gll_%02d.tab", 1)
"directory/gll_01.tab"
You can use this in your open statements too. Here they are in action:
julia> for i in 5:10
println("$i file is: $(#sprintf("directory/gll_%02d.tab",i))")
end
5 file is: directory/gll_05.tab
6 file is: directory/gll_06.tab
7 file is: directory/gll_07.tab
8 file is: directory/gll_08.tab
9 file is: directory/gll_09.tab
10 file is: directory/gll_10.tab

Read from txt file to matrix after a specific expression

I wanted via matlab to read a table of data from a txt file after a specific expression and a number of non desired lines for example the AA.txt have:
Information about students :
AAAA
BBBB
1 10 100
2 3 15
! ! ! a number of lines
10 6 9
I have like information the expression 'Information about students', the number of skipped lines 2 and the number of columns 3 and rows 10 in desired matrix.
if I understand correctly, you wanna skip the first 3 lines (assuming them as headers) and then reading the rest.
I would follow this procedure:
fid = fopen(filename,'r');
A = textscan(fid,'%f %f %f','HeaderLines',3,'Delimiter','\r\n');
I currently do not have access to MATLAB, but I do believe it will work.

How do I read and parse a .dat file in C?

I have a file called resistors.dat and I need to get my program to read and parse the values from the file into my program.
How would I read a file like this in C?
Read from the le resistors.dat (supplied on Blackboard) similarly to what you have done in Problem 2 of Lab 12. Each line in resistors.dat now represents one row: Ria, Rib and Ric (i = 1; 2; : : : ; n) of the circuit. Expand Problem 2 of Lab 12 to calculate the total resistance of the circuit. Hint: The total resistance is given by 1 R = 1 R1 + 1 R2 + 1 R3 + : : : + 1 Rn where Ri is the sum of resistances in one input row. In a loop, compute the sum of the inverse resistances 1=Ri. After the input has finished, compute the inverse of this sum to obtain the final result.
This is the content of resistors.dat:
64.35 35.52 85.37
90.43 12.99 80.40
98.37 32.63 78.42
3.82 82.74 52.61
3.75 72.47 49.05
96.73 16.07 23.46
48.15 36.62 83.64
51.96 27.19 22.38
4.18 46.07 91.21
96.94 8.17 50.45
0
There are several ways to accomplish this. I expect that your Resistors.dat file looks something like this:
r=1
r=20
r=22
r=2
I suggest you do something like this:
fopen to open the file, fgets in a while loop until the end of the file (!EOF), to read each line. Then use sscanf to parse each line.

Reading a text file in MATLAB line by line

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.
ABC,51.9358183333333,4.183255
ABC,51.9353866666667,4.1841
ABC,51.9351716666667,4.184565
ABC,51.9343083333333,4.186425
ABC,51.9343083333333,4.186425
ABC,51.9340916666667,4.18688333333333
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
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:
>> [numData,textData,rawData] = xlsread('input_file.csv')
numData = % An array of the numeric values from the file
51.9358 4.1833
51.9354 4.1841
51.9352 4.1846
51.9343 4.1864
51.9343 4.1864
51.9341 4.1869
textData = % A cell array of strings for the text values from the file
'ABC'
'ABC'
'ABC'
'ABC'
'ABC'
'ABC'
rawData = % All the data from the file (numeric and text) in a cell array
'ABC' [51.9358] [4.1833]
'ABC' [51.9354] [4.1841]
'ABC' [51.9352] [4.1846]
'ABC' [51.9343] [4.1864]
'ABC' [51.9343] [4.1864]
'ABC' [51.9341] [4.1869]
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:
index = sqrt(sum(numData.^2,2)) >= 50; % Find the rows where the point is
% at a distance of 50 or greater
% from the origin
xlswrite('output_file.csv',rawData(index,:)); % Write those rows to a new file
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.
You cannot read text strings with csvread.
Here is another solution:
fid1 = fopen('test.csv','r'); %# open csv file for reading
fid2 = fopen('new.csv','w'); %# open new csv file
while ~feof(fid1)
line = fgets(fid1); %# read line by line
A = sscanf(line,'%*[^,],%f,%f'); %# sscanf can read only numeric data :(
if A(2)<4.185 %# test the values
fprintf(fid2,'%s',line); %# write the line to the new file
end
end
fclose(fid1);
fclose(fid2);
Just read it in to MATLAB in one block
fid = fopen('file.csv');
data=textscan(fid,'%s %f %f','delimiter',',');
fclose(fid);
You can then process it using logical addressing
ind50 = data{2}>=50 ;
ind50 is then an index of the rows where column 2 is greater than 50. So
data{1}(ind50)
will list all the strings for the rows of interest.
Then just use fprintf to write out your data to the new file
here is the doc to read a csv : http://www.mathworks.com/access/helpdesk/help/techdoc/ref/csvread.html
and to write : http://www.mathworks.com/access/helpdesk/help/techdoc/ref/csvwrite.html
EDIT
An example that works :
file.csv :
1,50,4.1
2,49,4.2
3,30,4.1
4,71,4.9
5,51,4.5
6,61,4.1
the code :
File = csvread('file.csv')
[m,n] = size(File)
index=1
temp=0
for i = 1:m
if (File(i,2)>=50)
temp = temp + 1
end
end
Matrix = zeros(temp, 3)
for j = 1:m
if (File(j,2)>=50)
Matrix(index,1) = File(j,1)
Matrix(index,2) = File(j,2)
Matrix(index,3) = File(j,3)
index = index + 1
end
end
csvwrite('outputFile.csv',Matrix)
and the output file result :
1,50,4.1
4,71,4.9
5,51,4.5
6,61,4.1
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!

Resources