my code overwrites the .csv file - matlab

I got a critical problem. I am working on a code which should analyze data from an experiment. I got the data in a .csv file and tried import these data to work with them in MATLAB.
My problem is that my code imported the .csv data and overwrote my .csv file.
Can you help to find a way how to import the files without overwriting/erasing the original .csv file?
My code is here. Is this okay or shall I try something else?
fid = fopen('data.csv');
data = textscan(fid, '%*s %*f %f', 'delimiter', ';', 'headerlines', 5);
fclose(fid);

I don't see a problem with your posted code snippet, but the importdata function may be more convenient to use: http://www.mathworks.com/help/matlab/ref/importdata.html

Related

Write data to one file, numerous times

I have a very long data needs to be analyzed, and so I've decided to write it to a file several times during the code, and store it as a .csv file.
I've tried the following:
fid = fopen('c:\temp\results\final.csv','w'); %fid is a global variable
In another file do the following
string_data = strcat(fullFileName, '_' ,stim_vec,'_THD.csv');
fprintf(fid, '\n%s\n', string_data);
dlmwrite('c:\temp\results\final.csv', THD, '-append');
string_data = strcat(fullFileName, '_' ,stim_vec,'_phase_diff.csv');
fprintf(fid, '\n%s\n', string_data);
dlmwrite('c:\temp\results\final.csv', phase_diff, '-append');
And again in the first file
fclose(pid)
My problem is that data arriving from dlmwrite command only being written once, I think that fprintf data overrun it - but couldn't figure out how...
Can someone please assist me?
Thanks,

Matlab - error using textscan (invalid file identifier)

I need to run a script in Matlab (2015b). The critical part of this script is:
%Create module to divide up files
%Import Matlab processed data from Gaus script
Gaus_import_Name= strcat('MvsL\MvsL_Combined_OutputGaus.csv');
Summary_gausian_infomration_Name=strcat('MvsL\MvsL_Summary_Gausians_for_individual_proteins.csv');
%import data files Gaus data
f1 = fopen (Gaus_import_Name);
Processed_data1 = textscan(f1, '%s', 'Delimiter',',');
fclose(f1);
"MvsL\MvsL_Combined_OutputGaus.csv" and "MvsL\MvsL_Summary_Gausians_for_individual_proteins.csv" are files that already exist and this script needs to open them. All files needed and the script are in the same folder. After running this script, I get this message:
Error using textscan
Invalid file identifier. Use fopen to generate a valid file identifier.
Error in Alignment (line 127)
Processed_data1 = textscan(f1, '%s', 'Delimiter',',');
I have also tried
f1 = fopen (Gaus_import_Name, 'r+');
but it did not help.
Do you have any experience with this kind of error?

Matlab textscan formatspec delimiter error

When reading a large csv file Matlab doesn't recognize ||,|| as a proper delimiter as input argument for textscan. The data is as follows (simplified):
||X||,||Y||,||Z|| (header)
||1||,||2||,||4||
||4||,||4||,||3||
etc.
I use data = textscan(fileID,formatSpec,'Delimiter',','); to read in the data with some format spec '%f %f %f'.
My rubber band solution has been to use 010 editor to replace all '||' with '', making it a proper csv file for matlab, but due to the size of the document (6M lines with approx 35 fields) and the frequency of new documents this is hardly a great solution.
Does anyone know a proper way to import such a file?
You should be able to include it in the format specifier:
data = textscan(fid, '||%f||,||%f||,||%f||', 'headerlines', 1)
and then just leave out the delimiter.
Edit (Following on from comments)
If you are trying to read in strings, the trick is to get it to read in strings without the | character. This is done using %[^|], like this:
data = textscan(fid, '|| %[^|] ||,|| %[^|] ||,|| %[^|] ||', 'headerlines', 1)

Reading CSV files with MATLAB?

I am trying to read in a .csv file with MATLAB. Here is my code:
csvread('out2.csv')
This is what out2.csv looks like:
03/09/2013 23:55:12,129.32,129.33
03/09/2013 23:55:52,129.32,129.33
03/09/2013 23:56:02,129.32,129.33
On windows I am able to read this exact same file with the xlsread function with no problems. I am currently on a linux machine. When I first used xlsread to read the file I was told "File is not in recognized format" so I switched to using csvread. However, using csvread, I get the following error message:
Error using dlmread (line 139)
Mismatch between file and format string.
Trouble reading number from file (row 1u, field 2u) ==> /09/2013
23:55:12,129.32,129.33\n
Error in csvread (line 48)
m=dlmread(filename, ',', r, c)
I think the '/' in the date is causing the issue. On windows, the 1st column is interpreted as a string. On linux it seems to be interpreted as a number, so it tries to read the number and fails at the backslash. This is what I think is going on at least. Any help would be really appreciated.
csvread can only read doubles, so it's choking on the date field. Use textscan.
fid = fopen('out2.csv');
out = textscan(fid,'%s%f%f','delimiter',',');
fclose(fid);
date = datevec(out{1});
col1 = out{2};
col2 = out{3};
Update (8/31/2017)
Since this was written back in 2013, MATLAB's textscan function has been updated to directly read dates and times. Now the code would look like this:
fid = fopen('out2.csv');
out = textscan(fid, '%{MM/dd/uu HH:mm:ss}D%f%f', 'delimiter', ',');
fclose(fid)
[date, col1, col2] = deal(out{:});
An alternative as mentioned by #Victor Hugo below (and currently my personal go-to for this type of situation) would be to use readtable which will accept the same formatting string as textscan but assemble the results directly into a table object:
dataTable = readtable('out2.csv', 'Format', '%{MM/dd/uu HH:mm:ss}D%f%f')
dataTable.Properties.VariableNames = {'date', 'col1', 'col2'};
dataTable =
3×3 table
date col1 col2
___________________ ______ ______
03/09/2013 23:55:12 129.32 129.33
03/09/2013 23:55:52 129.32 129.33
03/09/2013 23:56:02 129.32 129.33
Unfortunately, the documentation for csvread clearly states:
M = csvread(filename) reads a comma-separated value formatted file, filename. The file can only contain numeric values.
Since / is neither a comma, nor a numeric value, it produces an error.
You can use readtable, as it will accept any input.
https://www.mathworks.com/help/matlab/ref/readtable.html
Yeah xlsread requires Microsoft Excel to be installed, unless it is run in 'basic' mode and 'basic' mode only reads .xls .xlsx and .xlsm files.
Another alternative are a number of user-written functions posted on MATLAB's file exchange that will work on linux and are more flexible at reading non formatted content.
One example:
https://www.mathworks.com/matlabcentral/fileexchange/75219-csv2cellfast-import-csv-files-on-machines-without-excel

how to textscan to read all the lines in a file

I am trying to read all the lines in a .m file with the following
file_content = textscan(fid, '%s', 'delimiter', '\n', 'whitespace', '')
but this just returns
file_content =
{0x1 cell}
when actually my file has 224 line. so if i use
file_content = textscan(fid,'%s',224,'delimiter','\n')
i get all the lines
file_content =
{224x1 cell}
what will be a more proper way to read all the data(mostly strings) in a .m file?
thanks
Since you do not list your needs (are you reading a huge file?, many small files? is speed an issue? what do you really want to do?) I'm giving you the simplest possible answer:
You do this:
f = fopen('data.txt');
g = textscan(f,'%s','delimiter','\n');
fclose(f);
remember to close after reading, because otherwise you won't be able to read again.
You can get the first line as g{1}{1}, the second as g{1}{2} and so on.
Here is the matlab documentation for textscan which gives a lot more details.
Here's a method that worked for me:
fid = fopen('filename','r'); %opening in read mode (default)
inter = textscan(fid,'%[^\n]');
lines = inter{1,1};
fclose(fid);
This command reads the whole file 'line by line'. for example, I had a text file with 1332 lines, this code creates a variable inter which is a {1,1 cell} and lines which is a [1x102944 char].
I'm not sure why/how this works (it'd be great if someone else reading this knows how!) but it works for my program.
That call to textscan means "read everything up to a \n".
In general your file may have mixed line endings, or none at all and have records separated by ':' or something.