Function inside for loop not working

P
Peter Hinderberger · Mar 3, 2022 · 1.8K views
Question
Hi, I am trying to iterate the diagonal filling of a previously defined, 0 valued hypercube object with a for loop of the following form:   EDIT - REMAINING CODE:   % Load and adjust test image, convert to .png (probably unnecessary) addpath('D:\Users\Peter\Desktop\Masterarbeit\Messdaten\Bilder_Einfach'); img = imread('BspBild_1.jpg'); imwrite(img, 'BspBild.png'); img = double(rgb2gray(imread('BspBild.png'))); img = rescale(img); sz = size(img); % Create empty hypercube zerocube = zeros(detformat_y, 2*detformat_x, n_lambda); hcube = hypercube(zerocube, wavelengths); % Add image to the first band hcube = assignData(hcube, 1:411, detformat_x/2+1:640+detformat_x/2, 1, img); % Show image as first hypercube band (for testing reasons) figure(2) imshow(hcube.DataCube(:, :, 1)) title('Cube-Ausschnitt') % loop to "shear" image in the wavelength direction EDIT - END for k = 1:n_lambda % n_lambda is the number of bands temp = img(:, k); % img is the monochromatic image that should fill a diagonal line hcube = assignData(hcube, :, k, k, temp); end the function assignData is defined in the Image Processing Toolbox Hyperspectral Imaging Library and works fine outside the for loop.   Whenever I try to execute the code it gives me "Unrecognized function or variable 'assignData'." in the line with this function.   Since directly indexing inside the hcube.DataCube object is not possible this function seems to be the only option to change hyperspectral data.  
Expert Answer
Profile picture of Neeta Dsouza
Neeta Dsouza PhD Expert
Answered Aug 11, 2026
One possible explanation for the observed behavior is that the variable hcube does not actually contain a hypercube object when the call to assignData happens inside your for-loop. Since we don't see the code before the for-loop, I can't really guess at why that might be. I recommend that you use the debugger and single-step through your code, making sure that the variables contain what you think they should contain at each step.
 
Also, does your real code include the single quotes around the colon character in the call to assignData? I don't this code will work as it is written here, without the single quotes.
100% Run Guarantee 3-Hour Fast-Track Delivery

Need a Custom Version or Complete Simulation for This Problem?

Our 500+ PhD engineers build, debug, and optimize working MATLAB scripts and Simulink (.slx) models tailored to your exact assignment rubrics with zero plagiarism.

Tested on MATLAB R2024b / R2026a
Turnitin 0% Plagiarism Report
Free 7-Day Revisions Guarantee
Have a different question? Ask here

Get a Free Consultation or a Sample Assignment Review!