Error message "using openfig" with Matlab 2018b and Octave - matlab

I am trying to run a teaching tutorial on Fourier space in MRI image generation, and consistently getting the following error:
Error using openfig
Too many input arguments.
Error in k_space_tutorial (line 20)
fig = openfig(mfilename, 'reuse')
I have made sure that the folder with the code and related files is in the directory:
>> isdir('k_Space_tutorial_David_Moratal')
ans =
logical
1
and in the path:
>> path
MATLABPATH
C:\Users\Myname\Documents\MATLAB
These are the contents of the directory:
>> dir
. half_fov.m openfig.m
.. image_test.bmp rectangular_matrix.m
README.txt imatge_i_espai_k_originals.m replay_pid10644.log
add_awgnoise.m k_space_tutorial.fig replay_pid11732.log
filtre_pas_alt.m k_space_tutorial.m replay_pid13344.log
filtre_pas_baix.m modaldlg.fig
half_fourier_fe.m modaldlg.m
half_fourier_pe.m motion_artifacts.m
Results of the debugger:

openfig is a function that comes with MATLAB, and according to its documentation, the syntax used is correct.
One of the possible reasons for a function being called correctly but leading to an error is that a different function is called inadvertently. This happens when a different function with the same name shadows (hides) the original function. An M-file in the current directory, or in an earlier directory on the path, with the same name will cause this.
Typing which openfig at the MATLAB command prompt will tell you what function is called when that name is used.
In this case, there is an M-file in the current directory with the same name. Deleting this file (or renaming it) will solve the problem.

Related

Failure to read jpeg files in Matlab

I'm trying to open images with imread but it keEps telling me that the files do not exist.
Here's the message from the command window
Error using imread>get_full_filename (line 516)
File "Pic1.jpg" does not exist.
Error in imread (line 340)
fullname = get_full_filename(filename);
Error in ImageDetection (line 2)
img1 = imread('Pic1.jpg');
And here are the sections of code that it's referencing from the function itself
if (fid == -1)
if ~isempty(dir(filename))
% String 'Too many open files' is from strerror.
% So, no need for a message catalog.
if contains(errmsg, 'Too many open files')
error(message('MATLAB:imagesci:imread:tooManyOpenFiles', filename));
else
error(message('MATLAB:imagesci:imread:fileReadPermission', filename));
end
else
error(message('MATLAB:imagesci:imread:fileDoesNotExist', filename));<--LINE 516
end
if isempty(fmt_s)
% The format was not specified explicitly.
% Get the absolute path of the file
fullname = get_full_filename(filename); <--LINE 340
Image isn't in Current Directory (Or Path)
If your image is in your working directory, you can call it by its name ("Pic1.jpg"). However, MATLAB doesn't search all folders on your computer. If, for example, if your program is running in C:\Users\user\Documents\MATLAB, and the image is in C:\Users\user\Pictures, you could reference the picture using:
Absolute paths ("C:\Users\user\Pictures\Pic1.jpg")
Relative paths ("..\..\Pictures\Pic1.jpg")
Usually, if the pictures only exist because of your program, it'd be somewhere in the same directory, so you wouldn't need to use ".." to move up any directories.
If you want the user to be able to select a picture each time the program is run, I'd recommend looking into uigetfile. If you want to know more about where MATLAB searches for files, see this article.
Secondly, you may want to check your file name. While it seems obvious, a simple misspelling can be hard to notice at times, for example "Pic1.jpg" vs "Pic1,jpg" vs "Pic1.jpeg"
Just above the line of your code where the error occurs, write a new line:
dir
To output in the prompt the files of the current folder, and check that the name of your file exactly appears there. Could you copy us that output?

Why do I need to run a file several times in MATLAB before it stops throwing an error?

I have been trying to debug this weird bug where I run a script in matlab and it says that it cannot find some function that I have clearly defined in a folder and also obviously imported to the running script. However, after running the script a few times it suddenly accepts it knows where the location of my function is and runs. I find this really strange because that shouldn't be happening because imports are deterministic functions, so I have no idea why running it multiple times should make a difference.
To reproduce my bug you can find my code in my project's github page. Go/cd to the folder:
research/HBF_mat_lib/HBF1_multivariant_regression/om_simulations/h_add_cv
and
run:
test_debug_script
in MATLAB.
It show throw an error:
Undefined function 'learn_HBF1_SGD' for input
arguments of type 'HBF1_parameters'.
Error in get_best_trained_hbf1_model (line 37)
[ mdl_params, errors_train, errors_test ] =
train_func( X_train, y_train, mdl_params,
iterations,visualize, X_test,y_test,
eta_c,eta_t, sgd_errors);
Error in test_debug_script (line 11)
get_best_trained_hbf1_model(slurm_job_id,
task_id)
Error in run (line 96)
evalin('caller', [script ';']);
but if you try running it enough times it runs as it should.
Things I've tried to clear this bug is clear my matlab environment, restore my path to normal, at this point I have no idea what I can do because I have also printed out what path is before executing stuff and the folder seems to be in my path variable. At this point I have no idea what I can do and why after running a few times it runs, it should ALWAYS fail but it does not...
I'd venture it's because you're creating a reference to learn_HBF1_SGD in simulation_config.m before you put the function on the Matlab path via load_paths.m, which you've made dependent on the function handle's content. Given the current structure, I'd switch to declaring train_func_name explicitly, and then use str2func to create the handle after the paths have been loaded.
Subsequent runs of the file work because the execution did not stop until after the calls to addpath have been made. And since addpath "adds the specified folders to the top of the search path for the current MATLAB® session", the function was on the Matlab path for subsequent runs.

Matlab Error: "Undefined function or method X for input arguments of type 'double'" With Recursion

I'm trying to make Sierpinski triangles with recursion but I get this error:
??? Undefined function or method 'sierpinski' for input arguments of type 'double'.
I understand that it has to do with Matlab not finding the path for my function, but the weird thing is that it can find my main sierpinski(x,y,n)-function but not the same function that I'm trying to call later in order to get recursion.
My code looks something like this:
function sierpinski(x,y,n)
...
sierpinski(x2,y2,n-1)
end
sierpinski([0,1,0.5],[0,0,1],4)
I would be very grateful if someone could help me with this :)
I cannot reproduce the first error you report. It probably has to do with the file not being on the path. The easiest way to avoid this is to change the working directory to the directory that contains the .m file.
The second error you describe in your comment is due to the fact that you're trying to have a file that is a Matlab function and a Matlab script at the same time. Both have the extension .m, but the first contains a function definition (something that can be called with arguments, has local variables, and can return values), and the other one contains a series of matlab statements that are to be executed exactly as if they were entered one by one in the command window.
Do the following:
– Make a Matlab function file sierpinski.m which includes only your function code:
function sierpinski(x,y,n)
hold on
if n == 0
fill(x,y,'r')
else
x2 = [(x(2)-x(1))/2, (x(2)-x(3))/2, x(3)+(x(2)-x(3))/2];
y2 = [y(1), y(3)/2, y(3)/2];
sierpinski(x2, y2, n-1)
end
Save the file to the current directory or a directory on the path.
– In the command window, enter the statement sierpinski([0,1,0.5],[0,0,1],2). The result is a figure window with a skewed red triangle. Not a Sierpinski triangle, but I guess the first step is done. ;-)
Instead of entering that statement in the command window, you can also make a Matlab script file. Edit a file with the name e.g. run_sierpinski.m, which contains the statement:
sierpinski([0,1,0.5],[0,0,1],2)
Again, save the file to the current directory or a directory on the path.
Now you can run the script, either by clicking the "Run" button in the GUI (green triangle or so), or by entering run_sierpinski in the command window. Either way, the result should be the same as entering the statement directly.

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..

How to make matlab see functions defined in .m files?

I'm a total newbie to MATLAB but I have to write some code in it. I've had problems with making MATLAB see functions I've defined in external .m files. This is what I've done: I've created a file named, say, foo.m in my home dir with the following contents:
function [y] = foo(x)
% description
y = x + 1
When I run matlab (my home dir is matlab's workdir) it does not see foo function - it replies with standard ??? Undefined function or variable 'foo' message. BUT help foo or which foo return correct data printing help text and pointing on foo.m file respectively.
I must be missing something but I have no idea what it is. This is getting very annoying.
Oh, after several trial and error attempts I've managed to call that function. Unfortunately I can't remember the sequence of steps I've performed. Moreover after restarting matlab it returns to its usual 'Undefined function or variable' response.
I have 7.11.0.584 matlab running on linux.
MATLAB needs to be told which directories to search over to access those m-files. Clearly it cannot be left to search over your entire disk drives. The MATLAB search path is a list of directories that will be searched in specific order to find your functions.
help addpath
help pathtool
You should never put those files anywhere in the official MATLAB toolbox directories. Choose an entirely separate directory.
Finally, be careful not to name your own functions to match the names of existing MATLAB functions. Otherwise, your very next question here will be why your code does not work properly. This is a common cause of strange and confusing bugs.
It seems you're having some trouble with addpath. Try opening the file in the matlab editor and adding a break point in the file. If the file is not on Matlab's path, matlab should ask if you want to change directory or add the file to the path, choose add to the path.
If this doesn't work, try changing the current working directory (displayed in the main window) to the same location as the m file and calling the function. If this doesn't work you're either getting the name wrong ar there's possibly something wrong with your installation.
Occasionally matlab has problems if it does not have write permission to the directory the file's in, so check that too, i.e. make sure admin rights aren't required for the directory or m file.
Oh, and try:
clear functions
to reload all functions into memory.
The function needs to be in MATLAB's path. Use pathtool to tell MATLAB where to find your function. Note that if you name a function the same name as an existing function, MATLAB will use whichever function it finds first according to the order that the paths are listed as you see them in pathtool.
Although coming late but I hope it will help someone.
If in the folder where the function you are calling is residing, there is any other function with the same name as one of the functions from MATLAB toolboxes, then Matlab will not recognize its license and therefore will disable the whole folder from execution, no matter it is properly added to the path. The help will display though.
In order to check it, type:
which name_of_func.m
and you will get the path with "%Has no license available" message.
If it is your own function, you should not get this message but only the path.
Therefore, find the function in this folder which has the same name as a MATLAB toolbox functions, and rename it. I will solve the problem :).
Best Regards
Wajahat