Not enough trace back to find error

6 views (last 30 days)
Gavin
Gavin on 15 Oct 2024
Commented: Umar on 25 Oct 2024
Apparently I have {} where I should have () I guess. I get this warning but the traceback list doesn't go far enough to find my code line:
Warning: The following error was caught while executing 'onCleanup' class destructor:
Brace indexing is not supported for variables of this type.
Error in cell2mat (line 36)
if isnumeric(c{1}) || ischar(c{1}) || islogical(c{1}) || isstruct(c{1})
^^^^
Error in onCleanup/delete (line 25)
obj.task();
^^^^^^^^^^
Error in onCleanup/delete (line 25)
obj.task();
^^^^^^^^^^
Error in matlab.apps.AppBase>@(source,event)executeCallback(ams,app,callback,requiresEventData,event) (line 60)
newCallback = @(source, event)executeCallback(ams, ...
^^^^^^^^^^^^^^^^^^^^^^^^
> In onCleanup/delete (line 25)
In matlab.apps.AppBase>@(source,event)executeCallback(ams,app,callback,requiresEventData,event) (line 60)
How do I go about finding it? dbstack doesn't work because the warning doesn't leave me in a place to use it.

Answers (1)

Umar
Umar on 15 Oct 2024

Hi @Gavin,

Glad to help you out again. After going through your comments and reviewing the documentation provided at the link below,

https://www.mathworks.com/help/matlab/ref/cell2mat.html

The error suggests that you are attempting to access an element of a variable using brace indexing { } but the variable does not support this operation. In MATLAB, brace indexing is used for accessing elements of cell arrays, while regular parentheses ( ) are used for accessing arrays or matrices.The function cell2mat expects a cell array where each cell contains data of the same type and dimension. If any cell contains a different type or an unsupported structure (like nested cells or objects), it will raise this warning. Here are steps listed below to identify the source of the error.

Check the Cell Array: Before calling cell2mat, inspect your cell array to ensure all cells contain compatible data types. You can do this with a loop:

     for i = 1:numel(C)
         disp(class(C{i}));
     end

Use Debugging Techniques: Since dbstack isn't providing useful context here, consider adding temporary breakpoints or using try-catch blocks around your code where cell2mat is called. For example:

     try
         A = cell2mat(C);
     catch ME
         disp(ME.message);
         % Optionally log or output more details about C
     end

Simplify Your Code: Isolate the segment of code that uses cell2mat. Create a minimal reproducible example where you suspect the error occurs and test it independently.

Example Analysis: Given your cell array example:

     C = {[1], [2 3 4];
          [5; 9], [6 7 8; 10 11 12]};

Running cell2mat(C) should work as all elements are numeric and can be concatenated. If you encounter issues, check if any modifications were made to C before this call.

If your data structure is complex or inconsistent, consider using alternative functions like vertcat or horzcat, which can handle concatenation without converting to a standard array.

By following these steps, you should be able to pinpoint the source of your warning and rectify any issues related to brace indexing and data compatibility in your MATLAB code.

Hope this helps.

Please let me know if you have any further questions.

  2 Comments
Gavin
Gavin on 24 Oct 2024
My problem was that with a 3000 line program and no hint as to where the error was (traceback stopped before it got to my code as I originally inricated). Cell2mat doesn't exist in my code. The whole {} vs () vs [] thing is a pain. For a language with no firm typing and the way it can change your objects from one type to another it's a bit hard to track down errors. The command window helps if you break out into the debugger and then type object names to see what they have become.
Eventually I found the errant () and now it's working but that doesn't answer my original question of how to get a longer traceback that goes all the way into my own code, not stopping in the AD generated code.
Umar
Umar on 25 Oct 2024

Hi @Gavin,

In MATLAB, the default behavior of the error traceback can indeed be limiting, especially in large programs. To enhance the traceback and include your own code, consider using the dbstop command. This command allows you to set breakpoints on errors, which can help you catch the error in your code directly.You can set a breakpoint for all errors by executing the following command in the Command Window:

dbstop if error

This will pause execution whenever an error occurs, allowing you to inspect the workspace and the call stack. You can then use the dbstack function to view the function call history, which can provide insights into where the error originated.

Hope this helps.

Sign in to comment.

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Products


Release

R2024b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!