MATLAB File I/O - matlab

I am trying to read in a .txt file that is a matrix and turn it into a row vector. However, I keep getting the error "Empty format string is not supported at the end of a file." Here is my code. Can anyone explain why I'm getting this?
filename = 'matrix.txt';
fID = fopen(filename);
M = csvread(filename);
rowVector = M(:);

Related

Open .mtx file in MATLAB

I have a .mtx file which contains a vector which I am suppose to use for matrix vector multiplication. I tried to open the file using
fopen('filename') but this does not work, it returns a single number. I have also tried using readmtx but this gives me the following error: File size does not match inputs. Expected a file size of 232316 bytes. Instead the file size is
365 bytes. Could you please advise how I can open and work with this type of file in MATLAB.
fopen('filename') outputs only the fileID, which is typically an integer >3. This fileID can be used with e.g. fscanf(fileID) to get the content of the file.
For the .mtx format, where each line contains [row-number column-number matrix-entry], you could also do something like:
%% file
filename = 'my_matrix.mtx';
%% read Matrix
Mat = read_mtx(filename);
%% function
function Mat = read_mtx(filename)
% open file
fID = fopen(filename,'r');
M = fscanf(fID, '%f');
% reshape M vector into Nx3 matrix
M = reshape(M, [3, length(M)/3])';
% assemble final matrix
Mat = zeros(M(end,1),M(end,1));
for ii = 1:size(M,1)
Mat(M(ii,1),M(ii,2)) = M(ii,3);
end
end

Issue with format specification while reading from file Matlab

I have a .dat file with a table containing data in following order:
0,000E+0 4,069E-2 -5,954E+0 1,851E-2
What I need to do is to read this data with matlab and then somehow handle it.
Here is my code:
path = 'C:/Users/user/Desktop/file1.dat';
fileID = fopen(path,'r');
formatSpec = '%e';
A = fscanf(fileID,formatSpec);
fclose(fileID);
disp(A);
Unfortunately, it doesn't work. What did I do wrong?
After replacement of comma with dot in data you can read it using dlmread function:
M = dlmread('filename', ' ');
M is what you want.
For the first part, replacing a character, you can use the following code:
% read the file
fid = fopen('input.txt','r');
f=fread(fid,'*char')';
fclose(fid);
%replace the char
f = strrep(f,',','.');
% write into the another file
fid = fopen('output.txt','w');
fprintf(fid,'%s',f);
fclose(fid);

matlab, Read multiple files .csv imported from a different directory

I am having a problem when I try to read multiple csv files from another directory. here I will show two options of my code:
clc;clear;
fileID = fopen('results.txt','w');
fprintf(fileID, 'Name\t\t\t\t\t\t\t\t\t%%variation\t\tSteady-state\n');
% s=dir('*.csv');
s=dir('C:\Users\michael\Documents\MATLAB\Data\*.csv');
for i = 1:length(s)
s1= strsplit(s(i).name,'.'); %split string from csv
s3 = char(strcat(s1(1),'.png'));%concatenate .png and convert to string
a = csvread(s(i).name,1,0);
ind = find(a(:,1)==60); %75% before end electrification
ind2 = find(a(:,1)==(60-60*0.75));
err = (a(ind,2)-a(ind2,2))/a(ind,2)*100; %variation
fig=figure;
plot(a(1:ind,1),a(1:ind,2),a(ind2:ind,1),a(ind2:ind,2),'*')
xlabel('time [s]');ylabel('Volume resistivity [ohm.cm]');
legend('y(x)', sprintf('variation = %0.1f%%',err),'Location','Southeast');
saveas(fig,s3)
for n = length(err)
if err<5
YoN = 'Yes';
else
YoN = 'No';
end
end
fprintf(fileID, '%s\t\t%0.1f%%\t\t\t\t%s\n', s(i).name, err, YoN);
end
fclose(fileID);
Basically when i let all the csv files in the working directory and untoggle the comment s=dir('*.csv'), it works properly, when I try to open them from the different directory, it works until the line a=csvred(s(i).name,1,0).
At that point it says:
Error using csvread (line 35)
File not found.
Error in Untitled (line 10)
a = csvread(s(i).name,1,0);
In both cases, s is a structure with 6 fields, and the field 'name' does exist in the structure.
Any suggestion?

Reading a line of binary file MATLAB

I am new in using MATLAB and I want to do a simple thing: I want to read a binary file that contains rows like this
32156432
345243867
454154351
35477
5641871
....
I know that the fread() in MATLAB reads the file byte by byte, but I want to read the value that there is on each line. All values are uint32_t and the file is generated with a script in C++ with just a printf, the values are printed in a file like my_file.bin launching the executable in this way ./executable param1 >> my_file.bin
You can use the function fscanf
Sample Code:
fileID = fopen('my_file.bin','w');
x = 32156432;
y = 345243867;
w = 454154351;
fprintf(fileID, '%d\n',x);
fprintf(fileID, '%d\n',y);
fprintf(fileID, '%d\n',w);
fclose(fileID);
fileID = fopen('my_file.bin','r');
formatSpec = '%d';
A = fscanf(fileID, formatSpec);

How to save multiple file (.mat) into .wav using MATLAB

I have a .mat file containing 100files. How to convert the 100 files one by one to .wav.
Every file contain vectors.I tried using this code but I got errors.
x=load('data_cropped.mat');
input_list = x;
for i = 1:length(input_list)
fid = fopen(input_list(i).name);
data = ' ';
fopen(fid);
wavwrite(data,16000,[input_list(i).name(1:length(input_list(i).name)-3),'wav']);
clear data
end
The error is:
>> convert_to_wav
Reference to non-existent field 'name'.
Error in convert_to_wav (line 7)
fid = fopen(input_list(i).name);
Please help me,
Thanks a lot
Assuming you know the sample rate of your audio then the snippet below should do what you want and give you a series of numbered wav files.
clear
load('data_cropped.mat');
data = whos;
fs = 44100 %change to your sample rate
for i = 1:length(data)
wavwrite(data(i).name,fs,num2str(i));
end