So I am trying to save a new search path.
I am trying to follow this example from Mathworks Mathworks example
My code is below. mypath is a 120 x 1 cell
path('mypath')
However when I run the code a pop up window says "Error using eval Underfined function 'workspacefunc' for input arguments of type 'struct'
When I try to check the path using the line below I get told the 'path may be bad'
checkPath = path;
The version they provide works with an array of strings, not a cell of strings. Hence, this should work:
mypath = char(mypath);
path('mypath');
Related
I'm using MATLAB R2017a and I would like to create a new binary file within the folder the script is running from.
I am running matlab as administrator since otherwise it has no permissions to create a file. The following returns a legal fileID:
fileID = fopen('mat.bin','w');
but the file is created in c:\windows\system32.
I then tried the following to create the file within the folder I have the script in:
filePath=fullfile(mfilename('fullpath'), 'mat.bin');
fileID = fopen(filePath,'w');
but I'm getting an invalid fileId (equals to -1).
the variable filePath is equal in runtime to
'D:\Dropbox\Studies\CurrentSemester\ImageProcessing\Matlab
Exercies\Chapter1\Ex4\mat.bin'
which seems valid to me.
I'd appreciate help figuring out what to do
The problem is that mfilename returns the path including the file name (without the extension). From the documentation,
p = mfilename('fullpath') returns the full path and name of the file in which the call occurs, not including the filename extension.
To keep the path to the folder only, use fileparts, whose first output is precisely that. So, in your code, you should use
filePath = fullfile(fileparts(mfilename('fullpath')), 'mat.bin');
I want to count the number of image in a folder using a GUI created in Matlab guide 2015b.
I have written this code:
Id = 3 (actually the value of id will be given by user at run time)
path =strcat ( ' c:\user\Desktop\New\TrainData\',Id)
path=strcat (path,'\')
d=dir (path)
n=length (d)
It shows the error that dir can not be used for cell input. This code is working when I use command prompt.
It shows error only when I want to use it through GUI. Initially I thought that it's a problem regarding the path.
So I displayed the path but it gave the perfect result.
I am confused. Kindly provide some solutions in Matlab
Instead of strcat you should use fullfile:
path = fullfile('c:\user\Desktop\New\TrainData',num2str(Id))
And be careful with dir, dir also list the subfolder so check that you only take into account the image file:
d = dir(path);
name = d(~[d.isdir]).name
Chances are you're getting your Id variable from a inputdlg or something. It is being read in as a cell array of strings rather than a string. You can check this using iscell:
iscell(Id)
% 1
You don't see any issues until you hit the dir command because strcat is able to handle this just fine but also yields a cell array of strings.
out = strcat('123', {'4'});
class(out)
% cell
If you read your error message thoroughly, the error explicitly states that the input to dir is a cell and not a string. The way to fix this is to first check if Id is a cell array and convert to a string if necessary.
Id = inputdlg('Enter an ID');
% Convert to a string if Id is a cell array
if iscell(Id)
Id = Id{1};
end
% Get a listing of all files/directories
d = dir(fullfile(folder, num2str(I)));
% Get number of files
nFiles = sum(~[d.isdir]);
Also, you don't want to try to concatenate a number with a string (strcat('abc', 1)) because this will convert the number to it's ASCII code. Instead you'll want to use num2str as shown above.
I am studying Suever's answer where I do not understand its application with fullfile in Code 1.
Code 1
filename=strcat('/Users/masi/Images/', 'p.1');
save(fullfile(filename,'.mat'),'time');
saveas(her, fullfile(filename,'.png'));
Output
Error using save
Cannot create '.mat' because '/Users/masi/Images/p.1' does not
exist.
Code 2
filename=strcat('/Users/masi/Images/', 'p.1');
save(strcat(filename,'.mat'),'time');
saveas(her, strcat(filename,'.png'));
Success!
Change made based on Daniel's answer
filenameMat=fullfile('/Users/masi/Images/', 'p.1', '.mat');
save(filenameMat,'time');
but still getting
Error using save
Cannot create '.mat' because '/Users/masi/Images/p.1.mat' does not
exist.
I do not understand.
Why is code 1 giving the error?
You are using fullfile for a wrong application. The documentation clearly explains the parameters:
Folder and file names, specified as strings and cell arrays of strings.
You input a filename without extension and a file extension, hat is not what fullfile is made for. It will insert a file separator between both:
>> fullfile('foo','.bar')
ans =
foo\.bar
fullfile would be the right function to construct filename.
I want to read some "xlsx" & "txt" files from directory and process on that. Names of the files are the random word.
so I used a function(getAllFiles) to obtain all directions of those files which I founded in this link: How to get all files under a specific directory in MATLAB?.
when I want to use those direction in dlmread or xlsread it give an error as bellow:
??? Error using ==> dlmread at 55
Filename must be a string.
the code is as follow :
fileList = getAllFiles('/home/Network/econimi/SSS')
A=dlmread(fileList(2));
how can I convert fileList to the string format?
The output of getAllFiles is a cell array. As stated in the manual 'Cell array indices in smooth parentheses refer to sets of cells' which means fileList(2) is a cell as well. To access an element of a cell array, use curvy brackets.
Try:
A=dlmread(fileList{2});
I have created a function in MATLAB & have saved it as an m file. When I run my function, it's fine. However using the Windows 7 scheduler it goes to run my function and gives the error message 'Undefined variable 'myMethod' or function 'myMethod.m'.
When I run the which('myMethod.m') it returns the correct folder so not sure what this error message is about?
The pwd method returns the correct address of where my function is too, C:\SomeFolder\MATLAB\Me
Probably its simply not finding the function because it is not on the path.
Assuming you can run builtin functions via the scheduler, try something like this:
p = path
save p
% save c:\ p
In case you cannot even find the saved file, use the last line instead.
Match the path with your files location and presumably the path does not contain the folder which holds your file.