i wrote that code
clear all;
clc;
addpath('C:\Users\John\Documents\MATLAB\code for yannis\anger(W)\');
h1 = dir('C:\Users\John\Documents\MATLAB\code for yannis\anger(W)\');
for i=3:numel(h1)
%disp(h1(i,1).name);
%disp(k);
three(h1(i,1).name);
end
and the three function is
function three(filename)
%disp(filename);
q = char(39);
filename = strcat(q,filename,q)
%disp(filename);
load(filename);
And i get that error:
Error using load
Unable to read file '03a01WaM.mat': No such file or directory.
Error in three (line 7)
load(filename);
Error in run_three (line 13)
three(h1(i,1).name);
i also wrote exist('03a01WaM.mat') and the function return 2
Does anyone has an idea, what am i doing wrong?
There are multiple issues with your code.
addpath is simply unnessecary.
You are using relative path, but not cd. You have to use the full path to access the files.
You are adding a apostrophe to the filename.
Correct code would be:
directory='C:\Users\John\Documents\MATLAB\code for yannis\anger(W)\'; %'
h1 = dir(directory);
for i=3:numel(h1)
filename=fullfile(directory,h1(i,1).name);
load(filename);
end
Related
I have a Matlab script where I produce a figure, and then create an eps file in my current directory using the command
print('myFile','-depsc'). Immediately following, I have: mypdf = eps2pdf('myFile').
I get the error message that 'Error while creating temporary eps file: ..... cannot be accessed or does not exist'.
Has anyone had a similar problem? Any suggestions what I might be doing wrong? I'm using Ubuntu and Matlab 2017a.
Here is an example code that I type into the command line. I get the error message which I stated above.
figure()
plot(linspace(1,100),linspace(1,100)) %Simple line
print('my_plot','-depsc') %Create eps file.
mypdf = eps2pdf('my_plot'); %Should produce mypdf in my current directory.
<error message prints>
This isn't a standard function. If you read the function you will see errStr that it returns for this.
function [ok,errStr] = read_epsfilecontent( epsFile )
% Reads the content of the eps file into epsFileContent
global epsFileContent
ok = 0;
errStr = [];
fh = fopen(epsFile,'r');
if fh == -1
errStr = ['File: ' epsFile ' cannot be accessed or does not exist'];
return
end
Then we figure out when fopen returns -1
fileID = fopen(filename) opens the file, filename, for binary read
access, and returns an integer file identifier equal to or greater
than 3. MATLAB® reserves file identifiers 0, 1, and 2 for standard
input, standard output (the screen), and standard error, respectively.
If fopen cannot open the file, then fileID is -1.
Which means please post some of your code so we can figure out why it cannot open your file.
Edit: After some work around and it wasn't necessary to download the code this is how I solved your problem. There is another implementation called eps2xxx
While running your code I received this error
Error while creating temporary eps file: *.eps - File:
C:\Users\Ryan\Documents\MATLAB*.eps cannot be accessed or does not
exist
Which lead me to the information in the documentation here.
% Create tmp file,...
[ok,errStr] = create_tmpepsfile(source,tmpFile,orientation);
if ~ok
status = ['Error while creating temporary eps file: ' epsFile ' - ' errStr];
if nargout, result = 1; end;
if nargout > 1, msg = status; else, disp(status); end;
And I read you needed GhostScript, I wasn't sure if I had this anyways. I downloaded it and gave the full pathway to GS like the following.
figure()
fullgspath = 'C:\Program Files\gs\gs9.23\bin\gswin64c.exe';
plot(linspace(1,100),linspace(1,100)); %Simple line
print('my_plot','-depsc');
eps2xxx('my_plot.eps',{'pdf'},fullgspath);
which created your nice little pdf here.
I am trying to convert all .tiff-files inside a certain folder to .jpg.
I have tried executing
ReadImgs('home/luisa/misc','*.tiff');
using the following function:
function X = ReadImgs(Folder,ImgType)
Imgs = dir([Folder '/' ImgType]);
NumImgs = size(Imgs,1);
image = double(imread([Folder '/' Imgs(1).name]));
for i=1:NumImgs,
[pathstr,name,ext] = fileparts(Imgs(i).name);
concatena=strcat(name,'.jpg');
imwrite(y,concatena);
end
end
But I get this error:
>> codigoPruebas
Index exceeds matrix dimensions.
Error in ReadImgs (line 4)
image = double(imread([Folder '/' Imgs(1).name]));
Error in codigoPruebas (line 7)
ReadImgs('home/luisa/misc','*.tiff');
How can I resolve this?
Check the output of dir, it does return an empty struct. That's because you passed an invalid path. It's /home/luisa/misc not home/luisa/misc. Absolute path start with / relative path not.
Some additional advices in writing robust code:
Instead of [Folder '/' ImgType] use fullfile(Folder,ImgType). It's more robust (avoids duplicate file separators) and os independent.
Use im2double instead of double to convert images. This automatically scales to 0...1
There are multiple things wrong with your solution:
The error you are getting is because you are trying to access Imgs(1), even though it is empty. This is because you supplied a wrong file path: home/luisa/misc instead of /home/luisa/misc
You only read the first image, as image = double(imread([Folder '/' Imgs(1).name])); is not inside the for-loop. (And only accesses Imgs(1) instead of Imgs(i))
imwrite(y,concatena); should use image instead of y, as y is never defined.
Implementing these changes will result in:
function convertAllToJpg(Folder,ImgType)
Imgs = dir(fullfile(Folder,ImgType));
for i=1:numel(Imgs)
oldFilename = fullfile(Folder, Imgs(i).name);
[~,name,~] = fileparts(Imgs(i).name);
newFilename = fullfile(Folder, strcat(name, '.jpg'));
imwrite(imread(oldFilename), newFilename);
end
end
This is my answer, you can use it in your Matlab as a function
suppose oldFolder is store your origin image type
newFolder is store your change image type
ImgType is your origin image type
and you can change jpg whatever you want.
function convertAllToJpg(oldFolder,newFolder,ImgType)
Imgs = dir(fullfile(oldFolder,ImgType));
for i=1:numel(Imgs)
oldFilename = fullfile(oldFolder, Imgs(i).name);
[~,name,~] = fileparts(Imgs(i).name);
newFilename = fullfile(newFolder, strcat(name, '.jpg'));
imwrite(imread(oldFilename), newFilename);
end
end
Currently trying to import the following via the following script:
files = dir('C:\Users\student\Desktop\pattern hw4\train\*.mat');
for i=1:length(files)
A = load(files(i)); % <-- line 7
end
I get the error though:
Error using load
Argument must contain a string.
Error in hw4 (line 7)
A = load(files(i));
After adding .name I get the error:
>> hw4
Error using load
Unable to read file 'class1_1.mat': no such file or directory.
Error in hw4 (line 7)
A = load(files(i).name);
Ok fixed and final answer/solution:
files = dir('C:\Users\student\Desktop\pattern hw4\train\*.mat');
dname='C:\Users\student\Desktop\pattern hw4\train\';
for i=1:length(files)
fname=fullfile(dname,files(i).name);
A = load(fname);
end
The load command is expecting the filename whereas dir returns a structure. Try:
A = load(files(i).name);
I think you need to use A = load(files(i).name); at line 7.
files is an array of structs
I am using a script which generate a collection of strings in a loop:
'folder1/im1'
'folder1/im2'
...
'folder1/im3'
I assign the string to a variable, when I try to execute the img = dicomread(file); function I get the following error:
Error using dicomread>newDicomread (line 164)
The first input argument must be a filename or DICOM info struct.
Error in dicomread (line 80)
[X, map, alpha, overlays] = newDicomread(msgname, frames);
Error in time (line 14)
img = dicomread(file);
However, using the command line I don't get errors: img = dicomread('folder1/im1').
The code is the next:
for i=1:6 %six cases
nameDir = strcat('folder', int2str(i));
dirData = dir(nameDir);
dirIndex = [dirData.isdir];
fileList = {dirData(~dirIndex).name}; % list of files for each directory
n = size(fileList);
cd(nameDir);
for x = 1:n(2)
img = dicomread(strcat(pwd(), '/', fileList(x)));
end
cd('..');
end
What could be the error?
You've figured it out by now, haven't you.
Based on what you've written, you test
img = dicomread('folder1/im1');
when what you are having trouble with is
img = dicomread(file);
You need to actually test the line you are having trouble with. I would recommend:
putting a break point in test.m a the line img = dicomread(file). When you get to that line you can see what file is equal to. Also do whos file to make sure it is of class char and not a cell array or something random.
If you still want help, edit your original post and show the code where you assign those strings to file and tell us what happens when you type img = dicomread(file) at the command prompt.
Matlab keep give me following error message :
??? Error using ==> dir
Argument must contain a string.
Error in ==> Awal at 15
x = dir(subDirs)
Below is my codes :
%MY PROGRAM
clear all;
clc;
close all;
%-----Create Database-----
TrainDB = uigetdir('','Select Database Directory');
TrainFiles = dir(TrainDB);
dirIndex = [TrainFiles.isdir];
[s subDirNumber] = size(dirIndex);
for i = 3:subDirNumber
subDirs = {TrainFiles(i).name};
subDirs = strcat(TrainDB,'\',subDirs);
x = dir(subDirs) %<-------Error Here
end
Is something wrong with the codes? Your help will be appreciated.
I'm sorry for my bad English.
The problem is with this line:
subDirs = {TrainFiles(i).name};
When you strcat on the next line, you are strcat-ing two strings with a cell containing a string. The result in subDirs is a cell containing a string which dir() apparently doesn't like. You can either use
subDirs = TrainFiles(i).name;
or
x = dir(subDirs(1))
I would recommend the first option.
When I run your code I get the error message:
??? Error using ==> dir
Function is not defined for 'cell' inputs.
What MATLAB is telling you is that when you call dir(subDirs) subDirs is a cell rather than a string which is what dir wants. Something like dir(subDirs{1,1}) will do what (I think) you want. I'll leave it to you to rewrite your code.
with subDirs = {TrainFiles(i).name}; you create a cell-array of stings. dir is not defined for that type. Just omit the {} around the name
BTW: Your code does not only list directories, but all files. Check find on the isdir attribute to get only directory's indices!