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

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?

Related

Matlab: Error using readtable (line 216) Input must be a row vector of characters or string scalar

I gave the error Error using readtable (line 216) Input must be a row vector of characters or string scalar when I tried to run this code in Matlab:
clear
close all
clc
D = 'C:\Users\Behzad\Desktop\New folder (2)';
filePattern = fullfile(D, '*.xlsx');
file = dir(filePattern);
x={};
for k = 1 : numel(file)
baseFileName = file(k).name;
fullFileName = fullfile(D, baseFileName);
x{k} = readtable(fullFileName);
fprintf('read file %s\n', fullFileName);
end
% allDates should be out of the loop because it's not necessary to be in the loop
dt1 = datetime([1982 01 01]);
dt2 = datetime([2018 12 31]);
allDates = (dt1 : calmonths(1) : dt2).';
allDates.Format = 'MM/dd/yyyy';
% 1) pre-allocate a cell array that will store
% your tables (see note #3)
T2 = cell(size(x)); % this should work, I don't know what x is
% the x is xlsx files and have different sizes, so I think it should be in
% a loop?
% creating loop
for idx = 1:numel(x)
T = readtable(x{idx});
% 2) This line should probably be T = readtable(x(idx));
sort = sortrows(T, 8);
selected_table = sort (:, 8:9);
tempTable = table(allDates(~ismember(allDates,selected_table.data)), NaN(sum(~ismember(allDates,selected_table.data)),size(selected_table,2)-1),'VariableNames',selected_table.Properties.VariableNames);
T2 = outerjoin(sort,tempTable,'MergeKeys', 1);
% 3) You're overwriting the variabe T2 on each iteration of the i-loop.
% to save each table, do this
T2{idx} = fillmissing(T2, 'next', 'DataVariables', {'lat', 'lon', 'station_elevation'});
end
the x is each xlsx file from the first loop. my xlsx file has a different column and row size. I want to make the second loop process for all my xlsx files in the directory.
did you know what is the problem? and how to fix it?
Readtable has one input argument, a filename. It returns a table. In your code you have the following:
x{k} = readtable(fullFileName);
All fine, you are reading the tables and storing the contents in x. Later in your code you continue with:
T = readtable(x{idx});
You already read the table, what you wrote is basically T = readtable(readtable(fullFileName)). Just use T=x{idx}

how to save multiple Cell array values in one .csv file

I have been working on making a database which contains images and their preset values and other important parameters. But unfortunately, I'm not being able to save the initial data of say 10 images in one .csv file. I have made the code that runs fine with creating .csv file but saving the last value and overwriting all the previous values. I gave also once modified that is comment down in the code using sprintf but it make .csv file for every iteration separately. But i want to make one .csv file containing 7 column with all the respective values.
My code is below and output of my code is attached Output.
Please someone guide me how to make single .csv file with 10 values for instance (could be increased to hundreds in final database) to save in 1 .csv file.
clc
clear all
myFolder = 'C:\Users\USER\Desktop\PixROIDirectory\PixelLabelData_1';
filePattern = fullfile(myFolder, '*.png'); % Change to whatever pattern you need
theFiles = dir(filePattern);
load('gTruthPIXDATA.mat','gTruth')
gTruth.LabelDefinitions;
for i=1:10
%gTruth.LabelData{i,1};
baseFileName = theFiles(i).name;
fullFileName = fullfile(myFolder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
imageArray = imread(fullFileName);
oUt = regionprops(imageArray,'BoundingBox');
Y = floor(oUt.BoundingBox);
X_axis = Y(1);
Y_axis = Y(2);
Width = Y(3);
Height = Y(4);
CLASS = gTruth.LabelDefinitions{1,1};
JPG = gTruth.DataSource.Source{i,1};
PNG = gTruth.LabelData{i,1};
OUTPUT = [JPG X_axis Y_axis Width Height CLASS PNG]
% myFile = sprintf('value%d.csv',i);
% csvwrite(myFile,OUTPUT);
end
Try fprintf (https://www.mathworks.com/help/matlab/ref/fprintf.html).
You will need to open your output file to be written, then you can append lines to it through each iteration
Simple example:
A = [1:10]; % made up a matrix of numbers
fid = fopen('test.csv','w'); % open a blank csv and set as writable
for i = 1:length(A) % loop through the matrix
fprintf(fid,'%i\n',A(i)); % print each integer, then a line break \n
end
fclose(fid); % close the file for writing

Read a value from multiple text files and write all into another file

I have a list of sub-folders and each sub-folder has a text file name called simass.txt. From each of the simass.txt files I extract a c{1}{2,3} cell data (as done in the code below) and write it to a file name features.txt in sequential form in single column.
I am facing a problem where at the end I only have a single value in features.txt, which I believe is due to the values being overwritten. I'm supposed to have 1000 values in features.txt since I have 1000 sub-folders.
What am I doing wrong?
clc; % Clear the command window.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
% Define a starting folder wherever you want
start_path = fullfile(matlabroot, 'D:\Tools\Parameter Generation\');
% Ask user to confirm or change.
topLevelFolder = uigetdir(start_path);
if topLevelFolder == 0
return;
end
% Get list of all subfolders.
allSubFolders = genpath(topLevelFolder);
% Parse into a cell array.
remain = allSubFolders;
listOfFolderNames = {};
while true
[singleSubFolder, remain] = strtok(remain, ';');
if isempty(singleSubFolder)
break;
end
listOfFolderNames = [listOfFolderNames singleSubFolder];
end
numberOfFolders = length(listOfFolderNames)
% Process all text files in those folders.
for k = 1 : numberOfFolders
% Get this folder and print it out.
thisFolder = listOfFolderNames{k};
fprintf('Processing folder %s\n', thisFolder);
% Get filenames of all TXT files.
filePattern = sprintf('%s/simass.txt', thisFolder);
baseFileNames = dir(filePattern);
numberOfFiles = length(baseFileNames);
% Now we have a list of all text files in this folder.
if numberOfFiles >= 1
% Go through all those text files.
for f = 1 : numberOfFiles
fullFileName = fullfile(thisFolder, baseFileNames(f).name);
fileID=fopen(fullFileName);
c=textscan(fileID,'%s%s%s','Headerlines',10,'Collectoutput',true);
fclose(fileID);
%celldisp(c) % display all cell values
cellvalue=c{1}{2,3}
filePh = fopen('features.txt','w');
fprintf(filePh,cellvalue);
fclose(filePh);
fprintf(' Processing text file %s\n', fullFileName);
end
else
fprintf(' Folder %s has no text files in it.\n', thisFolder);
end
end
The problem is in the permission you use in fopen. From the documentation:
'w' - Open or create new file for writing. Discard existing contents, if any.
Which means you're discarding the contents every time, and you end up only having the last value. The fastest fix would be changing the permission to 'a', but I would suggest adding some changes to the code as follows:
Creating cellvalue before the loop, and read c{1}{2,3} into this new vector/cell array.
Perform the writing operation once, after cellvalue is fully populated.
cellvalue = cell(numberOfFiles,1);
for f = 1 : numberOfFiles
...
cellvalue{f} = c{1}{2,3};
end
fopen(...);
fprintf(...);
fclose(...);

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, textscan error

I get an error when I run a Matlab code (attached), that continuosly check and opens two .txt files at a given time interval, (2 seconds) and depending on the result a value comparison between those two files, chose one or another and use it....
Those two files are constantly saved and updated by a Java script at the same time interval of 2 seconds.
All the files are located in the same path.
The error I get is: "invalid file identifier. use fopen to generate a valid file identifier
Error in KinectDEMelevation_with_filecomparison2 (line 36)
DEM1 =
textscan(fid2,formatSpec,'HeaderLines',6,'Delimiter','\b'); "
The code is:
DEM = GRIDobj('kinectDEM0.txt');
clims = [-30 140];
imagesc(DEM,clims);
colormap(jet);
hold on
while(1)
tic
% clear all
% clf
% load('MyColormaps','mycmap');
%% Get kinectDEM0 data
% Open 'kinectDEM0.txt'
fid1 = fopen('kinectDEM0.txt', 'r+');
% Read data in from the .txt file
formatSpec = '%n';
DEM0 = textscan(fid1,formatSpec,'HeaderLines',6,'Delimiter','\b');
% Extract data from DEM0
DEM0Data = DEM0{1,1}(200:100:287800,1);
% Close 'kinectDEM0.txt'
fclose(fid1);
%% Get kinectDEM1 data
% Open 'kinectDEM1.txt'
fid2 = fopen('kinectDEM1.txt', 'r+');
% Read data in from the .txt file
formatSpec = '%n';
DEM1 = textscan(fid2,formatSpec,'HeaderLines',6,'Delimiter','\b');
% Extract data from DEM1
DEM1Data = DEM1{1,1}(200:100:287800,1);
% Close 'kinectDEM1.txt'
fclose(fid2);
%% Compare data, a logical array return 1 for true (points that has been changed), 0 for false
test = eq(DEM0Data,DEM1Data);
num = sum(test); % numbers of point in the scene that has been changed
threshold = 2900; % after this threshold update the image
if num > threshold
DEM = GRIDobj('kinectDEM1.txt');
clf
clims = [-30 140];
imagesc(DEM,clims);
colormap(jet);
hold on
end
T = toc;
pause(2 - T);
end
How can I fix it?
Thanks
You should add some error checking to make sure the file:
exists
has been successfully opened
For the first, you can use exist:
r = exist('kinectDEM0.txt','file');
% will return 2 if file exists
For the second, you need to check fid is valid. If fopen cannot open the file, this will be -1. You may also use the second output for fopen, errmsg, for additional checking; errmsg should be an empty string if the file was opened successfully, otherwise it will return the system error message, such as "No such file or directory".
A simple example:
fid = -1
while fid == -1
fid = fopen('myfile.txt'); % attempt to open until success
end