csvread error when loading csv file from root directory - matlab

Below is the code I am working currently:
cd '/Users/K3iTH/Desktop/Ubuntu (Shared)/Trial/'
filename = 'slice_quarter0';
k = 1
inputfile = sprintf('%s.%d.csv','filename',k-1);
data = csvread(inputfile,1,0);
I am trying to load the .csv file exported in Ubuntu. The code is run until inputfile. When I try to run data = csvread(inputfile,1,0);, it stated file not found.
Any mistake I did?

If the file slice_quarter0.csv exists, and is located in your working directory then you should modify your sprintf call to
inputfile = sprintf('%s%d.csv',filename,k-1);
Otherwise, csvread will try to open a file named filename.0.csv, which will cause the error you have since it presumably does not exist.

Related

MATLAB writing text file "invalid permission" in UBUNTU

I'm trying to use MATLAB to write a text file in a folder other than "current folder" in UBUNTU. This folder is located in home directory so there would be no permission problem. I'm using the code that is shown below:
folder = '~/newFolder';
s1=fopen(folder,'newText.txt','w');
fprintf(s1,'hi')
fclose(s1);
But when I run the program, it shows invalid permission error. It would worth to mention that I've no problem when I try to write the text in the current folder. I also tried chmod 0777 -R ~/newFolder to modify the permission with no success.
What shall I do?
Thanks
If you take a look at to the fopen in Matlab documentation, you see that the second input argument in permission (that is related to the own function NOT your OS):
fileID = fopen(filename)
fileID = fopen(filename,permission)
But you are passing the filename as a second parameters.
you need to concatenate filepath and filename :
s1=fopen(strcat(folder, 'newText.txt'),'w');

reading several text files in a same script

I have a question for the reading of multiple files .txt in the same times and same script. I have a principal folder Matlab in which there are 7 subfolders Folder1 to Folder 7 in which of them there is un document file.txt.
I would like to read each of 'file.txt' in a script that I run in the curent folder Matlab. is there a fast way to do this? Or am I forced to do load file.txt for each folder.
You can use dir to list all your Folder. Then you can create the path of your file for each folder and load this file.
folder = dir('Folder*'); %list all the folder whose name start with 'Folder'
for ii = 1:length(folder)
s{ii} = fullfile(folder(ii).name,'text.txt'); %create the path for each file
load(s{ii});
end
In a for loop you can create the folder name:
for i = 1:n
name = ['folder',int2str(i)]
% then you can open and read the file
fileID = fopen([name,'\file.txt'])
data = fread(fileID)
% Don't forget to close the file
fclose(fileID)
end

Load file in matlab ralative to script location

I have a simple script in matlab and I want to load a file. It seems it only works if the file is in the same dir as the script. If I add the file to a directory it does not read it.
For example:
fileID = fopen('myfile','r' ,'n', 'US-ASCII');
but when I put myfile in files:
fileID = fopen('files/myfile','r' ,'n', 'US-ASCII');
or
fileID = fopen('./files/myfile','r' ,'n', 'US-ASCII');
I get a -1 as a fileID. File cannot be read.
As per the comments, this is happening because you most likely added the path of where the script was located to your MATLAB path but you did not add the subdirectory where the file was in to your path. This is why it can't find the file. Therefore to avoid this in the future, you need to physically change the directory (i.e. the Working Directory) of where MATLAB is currently operating to where your script is stored.
It is then where local referencing should work. You can do this by either using the cd function, going to the top of your MATLAB window where you see the directory listing, clicking on the arrow to the right and pulling a drop down menu to change the directory, typing the actual directory you want by clicking on any blank space in the directory listing to enable a text box:
... or if you are running the code in the MATLAB editor, it'll request that you change directories as the script you are trying to run is not currently located in the working directory.
You can also programmatically add the subfolders in your script directory using mfilename, fileparts, genpath and addpath:
[dir, ~, ~] = fileparts(mfilename('fullpath')); % locate your script directory
addpath(genpath(fullfile(dir))); % add the folder and all subfolders to Matlab search directory
% then load your file.
fileID = fopen('myfile','r' ,'n', 'US-ASCII')
If it is also important that all outputs should be placed within the same directory as your script file, you can cd to your script directory:
cd(dir)

how to compile an .m file which contains calling of another .exe file inside it

I have an .m file from which I call another .exe file on my machine with "system" statement. Now, I am going to use "deploytool" to create a console application. But, running this application couldn't run the .exe file inside it. Please tell me if there is any way to resolve this problem.
More detail:
These are the files which are used by main m.file: A.txt , B.xlsx, C.exe (I have put all of them in a folder). And this is part of m.file which utilizes these file:
1- m = xlsread('B.xls');
…
12- [status,cmdout] = system('C');
After running C.exe it should output a text file called “Out.txt” and then
…
13- fileID = fopen(‘Out.txt’);
14- links = textscan(fileID, ‘%s’, 7, ‘delimiter’, ‘,’);
…
But in the resulted standalone file when it reaches to line 12, can’t run C.exe! –
Thanks in advance!

How to give current directory as Path name while reading an Excel file?

I am trying to update an already existing Excel file "Test.xls" Now, instead of giving the complete path of the Test.xls file, I wish to know how to read the Test.xls file from current directory. I am using Eclipse in Windows environment. Below is my code:
my $Excel = Win32::OLE->GetActiveObject('Excel.Application')
|| Win32::OLE->new('Excel.Application', 'Quit');
my $Book = $Excel->Workbooks->Open("Test.xls"); #This line throws an error but works if I give complete path name as D:/eclipse/workspace/testing/Test.xls
my $Sheet = $Book->Worksheets(1);
foreach my $data (#ifrules)
{
$Sheet->Cells($row,$col)->{'Value'} =$data;
$row++;
}
$Book->Close;
I have tried various options like ./Test.xls, .\Test.xls and others too.
From what I found, you can use getcwd() to get the current working directory. You can then concatenate that with the name of the file within $Excel->Workbooks->Open() to give the current directory plus the name of the file.
Source Link
Explanation: The basic issue here is that you are passing the file name to Excel, which doesn't know about Perl's current working directory. That is why you have to find out the full path name and give it to $Excel->Workbooks->Open().