Simple imwrite does not write to file - matlab

I am trying to manipulate and save an image to file and it doesn't seem to be working from a function. It does, however, work in the command window. I have tried save, saveas, fprint, and others with no luck.
A = imread('contourSS.jpg'); B = rgb2gray(A);
imwrite(B, 'new_image.gif', 'gif');
Nothing shows up in the MATLAB directory when I run this code from a function, but it does show up in the MATLAB directory when I run it from the command window. Any ideas?
Thanks in advance.

Are you sure the you're saving the file to the correct directory? Try adding adding disp(pwd) to the function, it will display the directory that you are saving too.
Also its generally a good idea to use complete paths when saving a file. Consider changing your code to this:
imgDir = /home/user/image;
readfile = fullfile( imgDir, 'contourSS.jpg');
writefile = fullfile( imgDir, 'new_image.gif');
A = imread(readfild); B = rgb2gray(A);
imwrite(B, writefile, 'gif');

Related

Why do I need to change MATLAB path in order to read the files?

I am reading 50 files from a folder as follows:
list_of_files=dir(fullfile('/home/user/Desktop/MTP/schemes/o33smnpimp/data/', '*.dat'));
My problem is until & unless I have same exact folder opened as path in MATLAB path (one above the path window) this command won't work. What is the reason behind this? Actually there are multiple schemes and every time I need to run a particular scheme, I have to go to the data folder of that particular scheme. How can it be solved?
The issue is that you can get the list of files using the full path like you have but you ALSO need to specify the full path when you use it. For example, try changing your code to:
baseDir = '/home/user/Desktop/MTP/schemes/o33smnpimp/data/'; % <--- will use this twice
list_of_files=dir(fullfile(baseDir, '*.dat'));
for ind = 1:length(list_of_files)
myFilenameFull = fullfile(baseDir, list_of_files(ind).name); % <---- must use fullfile here too!
D1 = getData(myFilenameFull, 'stuff');
end

Matlab - printing in a directory

I am trying to print an eps file 'flow.eps' in folder 'figures'. This script is in folder 'auto'.
path1 = fullfile('Documents', 'MATLAB', 'auto', 'figures', 'flow.eps');
print(gcf, '-depsc', 'path1')
However, I am getting 'path1.eps' in 'auto'. It is working when written like this:
print(gcf, '-depsc', '~/Documents/Matlab/auto/figures/flow.eps')
I am trying the former one because I want my script to be compatible with both windows & unix.
The correct way to do the first method is:
path1 = fullfile('Documents', 'MATLAB', 'auto', 'figures', 'flow.eps');
print(gcf, '-depsc', path1)
path1, <---> 'path1'
I searched a lot for that answer, I tried many of them, but seems no one is correct.
Some suggested to change the directory to the specified path you want to save your picture, others suggested movefile command but they are time consuming.
The solution is simply, if you wanna print in any folder, the print command uses the file name as a complete path otherwise it is saved in current directory.
So I wanna save the figure to F:\Folder\Subfolder\filename , I just type
print(gcf,'F:\Folder\Subfolder\filename', '-depsc' )
Or
print('F:\Folder\Subfolder\filename', '-depsc' )

saving data from workspace to different dirrectory in matlab

I have a loop which my main script run through that. I wounder to save some of my variables in different directory every time that my loop is running. I have used following script but its not working:
for i=1:size(whisk, 1);
my codes is here and it creates variables in my workspace like [format, measurements].
the rest is what I wote to save this variables:
mkdir('C:\videos\results\', num2str(i));
dumvar=0; % As matlab has problem with data>2GB, then I use this 2 line code for saving
save('measurenments','dumvar','-v7.3');
save(fullfile('C:\videos\results\', num2str(i),'measurenments'));
clear all;
close all;
end
but Unfortunately its not work!!!!!!!!!!!
Any help would be appreciated.
Sam
Except that measurenments is wrongly spelled (correct spelling is measurements), there is not so strange that it does not work. The first call to save, saves the variable dumvar in the current folder, with the format v7.3. The second call to save, saves the whole workspace as a file fullfile('C:\videos\results\', num2str(i),'measurenments'). Try this,
save(fullfile('C:\videos\results\', num2str(i),'measurenments'),'dumvar','-v7.3');
However it seems as the folder fullfile('C:\videos\results\', num2str(i),'measurenments') does not exist since you only create the folder mkdir('C:\videos\results\', num2str(i))
. Then matlab cannot save anything there. Try either to save as fullfile('C:\videos\results\', [num2str(i),'measurenments']) or create the directory mkdir('C:\videos\results\', [num2str(i),'\','measurenments']);
`

octave load function

i'm trying to load a mat file from a subdirectory using the following code:
% filename_str is read from a text file
directoryname_str = "./data";
f = fullfile(directoryname_str, filename_str);
load(f);
when i run this sequence, load says it can't find the file...but when i copy or type the relative path and the file name by hand into an active octave session, everything works like a champ with no errors.
i assume this has something to do with how octave searches for mat files? if so, what's the correct environment variable or function call i need to make in order for this code to work?
thanks!
Are you sure that what you put into the variable f is the same as what you input manually in octave?
Are you also in the same directory? Because you're specifying relative paths, this should be the case.. you can get the current directory octave is in with pwd
And last of all, you can double check file existence in octave itself using exist
exist(f,'file')
If this returns false, there's definitely something wrong with your current directory, are something's very weird going on..

Matlab: How to know the name of the file are you using in the workspace?

sometimes I load a .m file to work in the workspace, but I usually forget what is the recent file I´ve opened. So in the same way that you write who and you can see the variables of the workspace I suppose there should be a command to know what is the .m file in which you are working.
Does anyone know a command like this?
Thank you so much.
For newer MATLAB versions, there is a way to get the fullpath of the file currently being edited in the Editor:
if verLessThan('matlab', '7.10')
%# not supported
fname = '';
elseif verLessThan('matlab', '7.12')
%# R2010a,R2010b: editorservices
fname = editorservices.getActiveFilename;
else
%# R2011a: matlab.desktop.editor API
fname = matlab.desktop.editor.getActiveFilename;
end
mfilename seemed to be the right choice, but it returns an empty string when called from the command line. Therefore you may check the 'command history' provided by the IDE.
For larger projects it most often makes sense to use MATLAB's object model or at least functions in order to structure your work. Working in the 'workspace' often results in unwanted side effects.
I do something like this to bootstrap runs:
%%
params.run = 'hairy.m';
params.hair = 10;
setup_defaults(params);
run_lots_of_code(params);
This has advantages as it hides the 'globals' in a single global (params), and the global tells you where they came from.