I have a MATLAB script that worked perfectly fine; there were no problems with it whatsoever. Then I moved it into the MATLAB toolbox and added it to the cache so I could run it easily from the command line.
However, now that I have moved it into the toolbox, I get these error messages! There were no changes to the script or anything. I moved all of the subfolders and other files related to the script with it.
I have no idea why it is giving me this error message. Even worse, it does not do it every time I run the script! Sometimes if I close MATLAB and open it again, the script runs fine. Sometimes all I have to do is click anything in the GUI and it works! But the next time it won't? Can you help me out here?
These are the two error messages it gives me:
??? Too many outputs requested. Most likely cause is missing [] around
left hand side that has a comma separated list expansion.
Error in ==> trials at 13
picture1 = files1.name;
Error in ==> semjudge>TRIAL_Callback at 285
trials;
??? Error using ==> waitfor
Error while evaluating uicontrol Callback
And:
??? Error using ==> nchoosek at 31
The first argument has to be a scalar or a vector.
Error in ==> semjudge>START_Callback at 194
combos = nchoosek(1:nFiles, 2);
??? Error using ==> waitfor
Error while evaluating uicontrol Callback
What causes these errors, such that they only SOMETIMES appear (without any change in the .m file or the GUI or anything ...)?
This is frustrating me to no end. It was working perfectly fine, and stopped despite no changes being made. And it is inconsistent whether or not it gives me the error. I can't find any pattern to when it does work, and when it doesn't. And neither of the errors that it DOES give me make any sense to me.
The .m file(s) are too long to post here, so you can see them, here:
http://textuploader.com/?p=6&id=cKokK (semjudge.m)
http://textuploader.com/?p=6&id=vB9sD (trials.m)
It seems your script is not always able to find certain image files.
In trials.m files1 is probably an empty struct array so the assignment to picture fails. This can be caused by dir not finding appropriate files.
In semjudge.m, if there are no files, nFiles is 0, so 1:nFiles is the empty array.
Related
I'm extremely new to both working on Linux and Fortran, so apologies if this is a basic question.
I am trying to firstly use fwrite to save a 60x150 array that I've produced in MATLAB as a binary file, which I am then attempting to load and read in Fortran as a 60x150 array again.
In Matlab, I have used the following code to save the array. In this case the name of the array in the workspace is VP, and I'm saving it to a file also called 'VP':
>> fileID = fopen('VP','w');
>> fwrite(fileID,VP,'real*8');
>> fclose(fileID)
Next, I am copying the file over from Windows to a linux ssh server (I'm not sure if this is relevant, but thought it's worth including anything that might help).
Now, in my Fortran code, I've got:
REAL(KIND=kind(1.0D0)), DIMENSION(60,150) :: VP
...
open(unit, file="LOCATION/VP", access = "stream", form = "unformatted", iostat = stat)
if(stat /= 0) labort("Failed to open input file")
print *, wl
DO inx2=1,60
DO inx=1,150
print *,inx
READ(unit,*) VP(inx2,inx)
ENDDO
ENDDO
print *,VP(1:10,1)
Now, when I compile this there are no errors. However, when I run it, it gets to exactly the first "READ(unit,*) VP(inx2,inx)" before failing (you can tell from the print just before it).
I get the error:
forrtl: severe (257): formatted I/O to unit open for unformatted transfers, unit 111, file LOCATION/VP
Obviously I would like my actual result to be the function running and ending up with the same values in the array.
Now I've seen the question before, specifically for this error message, but that was answered by including access="stream" which I have already. Basically I am not sure at what point I am getting something wrong in this process, any help would be appreciated.
Note some things I have tried is changing the precision in fwrite, and swapping the inx2 and inx values around (but it fails on the first one so I don't believe that's the error).
Again this might be just a fundamental issue with my understanding of Fortran because I've been thrown in the deep end a bit with a project I'm working on (most of the code I'm running was produced by someone else, I'm just trying to edit a small part of it).
Edit:
Okay, thank you so much francescalus! He found the solution was to edit the line to READ(unit) VP(inx2,inx) in order to get it to run. However, the values I get by running the next line of code:
print *,VP(1:10,1)
Only the first value matches the first value in my original matlab array. Displaying VP(1:5,1:5) of the matlab array there aren't any other matching values. I might be able to figure this out on my own but as I'm already here I might as well ask as I haven't fully completed the original question (although got over a big hurdle!).
Edit 2:
Okay the next bit I've managed to figure out for myself. If anyone is searching though it was simply a case of swapping the DO loops. ie
DO inx=1,150
DO inx2=1,60
print *,inx
READ(unit,*) VP(inx2,inx)
ENDDO
ENDDO
Thanks for the help.
main.m goes
a
b
b.m goes
Line 1 to 489 (Many time-consuming codes are here)
Line 490 wi=function_example(fff,w0,factor,10);
But I got an error at line 490 of b.m
In this case, I want to retry
wi=function_example(fff,w0,factor,30);
right at the command window.
If I change the code itself and rerun the whole codes, it will take too much time (hours or even days) even to reach to the 490th line.
But since fff, w0, and factor were defined in b.m, fff, w0, and factor are not global variables.
So if I just type
wi=function_example(fff,w0,factor,30);
then MatLab will say
Undefined function or variable 'fff'.
So I am curious about how to run just one intermediate line at command window, rather than changing the code itself and running the whole code again which takes too much time.
First you want to have MATLAB automatically start debugging when there is an error. You can easily do this by typing the following before running your script.
dbstop if error
Once an error occurs, MATLAB will automatically start debugging at the line where the error occurred and you will have access to all variables as needed.
Here is some more information about how to examine the variables during debugging.
Probably the easiest way is to save the workspace right before that line and test it afterwards:
Line 1 to 489 (Many time-consuming codes are here)
Line 490 save
Line 491 wi=function_example(fff,w0,factor,10);
Afterwards, in command-line you just need to run:
load
wi=function_example(fff,w0,factor,10);
That is most probably not possible..since the variables like fff,w0 etc. are declared and defined only when you run the script. What you can do is to call the function with appropriate values and see if the function is returning the correct value. For example say function_example(1,2,3,30) should return 40. See if the function returns the correct value. Bottom line - Test the function with known input values and see if it returns the correct result.
In MATLAB, how do you tell where in the code a variable is getting output?
I have about 10K lines of MATLAB code with about 4 people working on it. Somewhere, someone has dumped a variable in a MATLAB script in the typical way:
foo
Unfortunately, I do not know what variable is getting output. And the output is cluttering out other more important outputs.
Any ideas?
p.s. Anyone ever try overwriting Standard.out? Since MATLAB and Java integration is so tight, would that work? A trick I've used in Java when faced with this problem is to replace Standard.out with my own version.
Ooh, I hate this too. I wish Matlab had a "dbstop if display" to stop on exactly this.
The mlint traversal from weiyin is a good idea. Mlint can't see dynamic code, though, such as arguments to eval() or string-valued figure handle callbacks. I've run in to output like this in callbacks like this, where update_table() returns something in some conditions.
uicontrol('Style','pushbutton', 'Callback','update_table')
You can "duck-punch" a method in to built-in types to give you a hook for dbstop. In a directory on your Matlab path, create a new directory named "#double", and make a #double/display.m file like this.
function display(varargin)
builtin('display', varargin{:});
Then you can do
dbstop in double/display at 2
and run your code. Now you'll be dropped in to the debugger whenever display is implicitly called by the omitted semicolon, including from dynamic code. Doing it for #double seems to cover char and cells as well. If it's a different type being displayed, you may have to experiment.
You could probably override the built-in disp() the same way. I think this would be analagous to a custom replacement for Java's System.out stream.
Needless to say, adding methods to built-in types is nonstandard, unsupported, very error-prone, and something to be very wary of outside a debugging session.
This is a typical pattern that mLint will help you find:
So, look on the right hand side of the editor for the orange lines. This will help you find not only this optimization, but many, many more. Notice also that your variable name is highlighted.
If you have a line such as:
foo = 2
and there is no ";" on the end, then the output will be dumped to the screen with the variable name appearing first:
foo =
2
In this case, you should search the file for the string "foo =" and find the line missing a ";".
If you are seeing output with no variable name appearing, then the output is probably being dumped to the screen using either the DISP or FPRINTF function. Searching the file for "disp" or "fprintf" should help you find where the data is being displayed.
If you are seeing output with the variable name "ans" appearing, this is a case when a computation is being done, not being put in a variable, and is missing a ';' at the end of the line, such as:
size(foo)
In general, this is a bad practice for displaying what's going on in the code, since (as you have found out) it can be hard to find where these have been placed in a large piece of code. In this case, the easiest way to find the offending line is to use MLINT, as other answers have suggested.
I like the idea of "dbstop if display", however this is not a dbstop option that i know of.
If all else fails, there is still hope. Mlint is a good idea, but if there are many thousands of lines and many functions, then you may never find the offender. Worse, if this code has been sloppily written, there will be zillions of mlint flags that appear. How will you narrow it down?
A solution is to display your way there. I would overload the display function. Only temporarily, but this will work. If the output is being dumped to the command line as
ans =
stuff
or as
foo =
stuff
Then it has been written out with display. If it is coming out as just
stuff
then disp is the culprit. Why does it matter? Overload the offender. Create a new directory in some directory that is on top of your MATLAB search path, called #double (assuming that the output is a double variable. If it is character, then you will need an #char directory.) Do NOT put the #double directory itself on the MATLAB search path, just put it in some directory that is on your path.
Inside this directory, put a new m-file called disp.m or display.m, depending upon your determination of what has done the command line output. The contents of the m-file will be a call to the function builtin, which will allow you to then call the builtin version of disp or display on the input.
Now, set a debugging point inside the new function. Every time output is generated to the screen, this function will be called. If there are multiple events, you may need to use the debugger to allow processing to proceed until the offender has been trapped. Eventually, this process will trap the offensive line. Remember, you are in the debugger! Use the debugger to determine which function called disp, and where. You can step out of disp or display, or just look at the contents of dbstack to see what has happened.
When all is done and the problem repaired, delete this extra directory, and the disp/display function you put in it.
You could run mlint as a function and interpret the results.
>> I = mlint('filename','-struct');
>> isErrorMessage = arrayfun(#(S)strcmp(S.message,...
'Terminate statement with semicolon to suppress output (in functions).'),I);
>>I(isErrorMessage ).line
This will only find missing semicolons in that single file. So this would have to be run on a list of files (functions) that are called from some main function.
If you wanted to find calls to disp() or fprintf() you would need to read in the text of the file and use regular expresions to find the calls.
Note: If you are using a script instead of a function you will need to change the above message to read: 'Terminate statement with semicolon to suppress output (in scripts).'
Andrew Janke's overloading is a very useful tip
the only other thing is instead of using dbstop I find the following works better, for the simple reason that putting a stop in display.m will cause execution to pause, every time display.m is called, even if nothing is written.
This way, the stop will only be triggered when display is called to write a non null string, and you won't have to step through a potentially very large number of useless display calls
function display(varargin)
builtin('display', varargin{:});
if isempty(varargin{1})==0
keyboard
end
A foolproof way of locating such things is to iteratively step through the code in the debugger observing the output. This would proceed as follows:
Add a break point at the first line of the highest level script/function which produces the undesired output. Run the function/script.
step over the lines (not stepping in) until you see the undesired output.
When you find the line/function which produces the output, either fix it, if it's in this file, or open the subfunction/script which is producing the output. Remove the break point from the higher level function, and put a break point in the first line of the lower-level function. Repeat from step 1 until the line producing the output is located.
Although a pain, you will find the line relatively quickly this way unless you have huge functions/scripts, which is bad practice anyway. If the scripts are like this you could use a sort of partitioning approach to locate the line in the function in a similar manner. This would involve putting a break point at the start, then one half way though and noting which half of the function produces the output, then halving again and so on until the line is located.
I had this problem with much smaller code and it's a bugger, so even though the OP found their solution, I'll post a small cheat I learned.
1) In the Matlab command prompt, turn on 'more'.
more on
2) Resize the prompt-y/terminal-y part of the window to a mere line of text in height.
3) Run the code. It will stop wherever it needed to print, as there isn't the space to print it ( more is blocking on a [space] or [down] press ).
4) Press [ctrl]-[C] to kill your program at the spot where it couldn't print.
5) Return your prompt-y area to normal size. Starting at the top of trace, click on the clickable bits in the red text. These are your potential culprits. (Of course, you may need to have pressed [down], etc, to pass parts where the code was actually intended to print things.)
You'll need to traverse all your m-files (probably using a recursive function, or unix('find -type f -iname *.m') ). Call mlint on each filename:
r = mlint(filename);
r will be a (possibly empty) structure with a message field. Look for the message that starts with "Terminate statement with semicolon to suppress output".
I'm minimizing a function with the fmincon routine.
This function uses the integral command several times. However, some of those integrals turns out to be Inf or NaN and I don't want MATLAB to show a warning when this happens (the function is always finite).
I've tried using the command warning('off','MATLAB:integral:NonFiniteValue') but it don't seem to be working when running the optimization.
It could be you're simply suppressing the wrong message. You could inspect the values of
[a,b] = lastwarn
inside an output function (opts = optimset('OutputFcn', #myOutFcn);) to make 100% sure you're killing the correct warning message.
But I too have encountered this annoying behavior before -- you just can't seem to suppress certain warnings in MATLAB's own functions. For those, you have to resort to ugly and fragile hacks.
You could try
warning off
...
warning on
which suppresses all warnings for all code contained in the '...' section.
You could also use an undocumented feature: temporarily promote the warning to an error:
ws = warning('error', 'MATLAB:integral:NonFiniteValue');
...
warning(ws);
and wrap it up in a try....catch. Chances are you then interrupt integral and thus fmincon prematurely and will thus have to wrap it up together with some rescue mechanism, but that gets real complicated and real ugly real fast, so that's only to be used as a last resort...
...so in all, it's easiest to just live with the warnings.
There's an alternative way: click on the uppermost link in inner matlab files that produced that warning, find warning ID & copy it into clipboard.
Then add the following line to your script:
warning('off','IDHERE');
Replace IDHERE with actual ID
See https://tushev.org/articles/blog/17/how-to-suppress-matlab-warnings
As the title says, I want to have a parfor loop with inside usage of arrayfun.
I created a minimal working example for the problem:
Having the following lines in a file called thisparfortest.m
function test=thisparfortest(countmax)
parfor count=1:countmax
test(count).nummer=count;
test(count).bisdrei=arrayfun(#(testnum)eq(test(count).nummer,testnum),1:3);
end
the command mcc('-e','-v','thisparfortest') results in
Compiler version: 4.18.1 (R2013a)
Error: File: **************\thisparfortest.m Line: 3 Column: 5
The variable test in a parfor cannot be classified.
See Parallel for Loops in MATLAB, "Overview".
Processing C:\Program Files\MATLAB\R2013a\toolbox\matlab\mcc.enc
Processing include files...
2 item(s) added.
Processing directories installed with MCR...
The file mccExcludedFiles.log contains a list of functions excluded from the CTF archive.
0 item(s) added.
Generating MATLAB path for the compiled application...
Created 43 path items.
Parsing file "****************\thisparfortest.m"
(Referenced from: "Compiler Command Line").
Parsing file "C:\Program Files\MATLAB\R2013a\toolbox\compiler\deploy\deployprint.m"
(Referenced from: "Compiler Command Line").
Parsing file "C:\Program Files\MATLAB\R2013a\toolbox\compiler\deploy\printdlg.m"
(Referenced from: "Compiler Command Line").
Unable to determine function name or input/output argument count for function
in MATLAB file "thisparfortest".
Please use MLINT to determine if this file contains errors.
Error using mcc
Error executing mcc, return status = 1 (0x1).
but as advised mlint thisparfortest (and also checkcode) returns no problems - like in the editor.
The loop can be done and compiled as a for loop.
Please do not ask for the sense of these commands - they are just here for the mwe.
I think, this should be reported to mathworks - or have I done something wrong?
Some additions:
When running
function retval=thisparfortest(countmax)
helpfun=#(x)arrayfun(#(testnum)eq(x,testnum),1:3);
parfor count=1:countmax
retval(count).nummer=count^2;
retval(count).bisdrei=helpfun(retval(count).nummer);
end
with only the for loop it works, but when using the shown version with parfor it results in
Error using thisparfortest>(parfor supply) (line 3)
Undefined function or variable "retval".
Error in thisparfortest (line 3)
parfor count=1:countmax
Caused by:
Undefined function or variable "retval"
Shouldn't that be caught by mlint/checkcode? This happens without compiler.
I don't believe the issue has anything to with compilation. When I attempt to run your code in regular MATLAB I get the same error that the variable test in a parfor cannot be classified.
There's no bug here - not every piece of code can be run inside a parfor loop, and it's not possible for MATLAB to perfectly determine prior to runtime which pieces can and can't. It tries to do a good job, and when it does, the Code Analyzer will tell you prior to runtime - but when it can't, it will give a runtime error as you've found.
Perhaps you can think of a way that MATLAB could have statically determined that this variable could not be classified - in that case this could be reported to MathWorks as an enhancement request to Code Analyzer.