Ones function printing out

Can anyone tell me why my ones function is causeing my code to print even though it has a semi colon? I commented everything else out to narrow it down to the ones function. I already reinstalled matlab but nothing seems to be working
getX1();
Error using load
Unable to find file or directory 'mnist.mat'.

Error in solution>getX1 (line 7)
load('mnist.mat', 'images'); % Load 'images' variable from 'mnist.mat'
function X1 = getX1()
clc;
close;
clear;
load('mnist.mat', 'images'); % Load 'images' variable from 'mnist.mat'
num_images = size(images, 1);
image_size = 26 * 26; % Adjusted image size
X1 = ones(num_images, image_size + 1); % Initialize X matrix with bias column
for i = 1:num_images
%Crop 1-pixel border
image_cropped = images(i, 2:end-1, 2:end-1);
%Flatten image, normalize, and append 1
image_flat = double(reshape(image_cropped, 1, [])) / 255;
image_with_bias = [image_flat, 1];
% Update X matrix
X1(i, :) = image_with_bias;
end
% size of X matrix
fprintf('Size of X matrix: %d rows x %d columns\n', size(X1, 1), size(X1, 2));
end

11 comentarios

Walter Roberson
Walter Roberson el 24 de Feb. de 2024
We need the .mat file to test with.
It would be interesting to see the output of
which -all ones
Zachary David
Zachary David el 24 de Feb. de 2024
sorry it says its too large
Zachary David
Zachary David el 24 de Feb. de 2024
which -all ones
please.
It is a little suspect that images is uint32 but that you are dividing by 255
Zachary David
Zachary David el 24 de Feb. de 2024
im assuming you are asking which ones function? X1 = ones(num_images, image_size + 1);
we were told we needed to normalize the data and we were told to use 255
At the MATLAB command line, please give the command
which -all ones
and tell us the result.
This will give us information about whether possibly a different ones function is being invoked.
Stephen23
Stephen23 el 24 de Feb. de 2024
Editada: Stephen23 el 24 de Feb. de 2024
"im assuming you are asking which ones function?"
Yes. Please run this code:
which -all ones
and show us exactly what it displays.
Zachary David
Zachary David el 24 de Feb. de 2024
built-in (C:\Program Files\MATLAB\R2023b\toolbox\matlab\elmat\ones)
-this is what it gives me
VBBV
VBBV el 25 de Feb. de 2024
It seems you are running the script file getX1.m from a wierd location. Change its location to a different folder and run again
The path from the screenshot shows something like this
C:\users\zach1\OneDrive\...\Project1\mnist.mat\getX1.m
mnist.mat >> change this to a different name without a dot and check whether you still get that issue
Walter Roberson
Walter Roberson el 25 de Feb. de 2024
That will not make any difference. mnist.mat is a valid directory name.

Iniciar sesión para comentar.

Respuestas (2)

Walter Roberson
Walter Roberson el 24 de Feb. de 2024

1 voto

The ones is the output of your function. You are running the function by pressing the green Run button, which executes the function which returns X1 which then gets printed out automatically.

2 comentarios

Zachary David
Zachary David el 24 de Feb. de 2024
I know it is the output but why would it be printed out automatically when im not asking for that to happen. How do i stop it from printing out?
At the end of the code, insert
if nargout == 0
clear X1;
end

Iniciar sesión para comentar.

Star Strider
Star Strider el 24 de Feb. de 2024
The fprintf call will execute and print even if there is a semicolon at the end of that call.
X1 = rand(3,5);
fprintf('Size of X matrix: %d rows x %d columns\n', size(X1, 1), size(X1, 2));
Size of X matrix: 3 rows x 5 columns
Are you seeing that or something else?
.

6 comentarios

Star Strider
Star Strider el 24 de Feb. de 2024
The ‘Workspace’ window reports a (677 x 60000) double matrix called ‘ans’, the default variable name MATLAB uses. I suspect that it’s not being generated by the function, and it may be coming from somewhere else, althoough I can’t determine that from here.
What results if you preallocate ‘X1’ with zeros instead? You’re overwriting it in the loop, so the sort pf preallocation might not matter.
Zachary David
Zachary David el 24 de Feb. de 2024
oddly the same thing
Star Strider
Star Strider el 24 de Feb. de 2024
Do you mean a matrix of zeros now, or still a matrix of ones?
Zachary David
Zachary David el 24 de Feb. de 2024
still a matrix of ones
Zachary David
Zachary David el 24 de Feb. de 2024
Editada: Zachary David el 24 de Feb. de 2024
i was thinking it might be coming from somewhere else too. Maybe my recycling bin
Star Strider
Star Strider el 24 de Feb. de 2024
It’s coming from somewhere else in your code. I doubt if anything in the recycling bin is in any way active.
I don’t know how long your code is, however in the past when I needed to isolate something like that, I inserted:
fprintf('Stopped on line %d\n', line_nr)
return
and then moved those lines to various points in my code and then looked to see if the problem recurred. (I inserted the line number manually.) That helped me find the problem.
There are a collection of debugging tools available, however they might not be helpful in tracking down a line that prints a matrix, and that’s all you want to do at the moment.

Iniciar sesión para comentar.

Categorías

Más información sobre Function Creation en Centro de ayuda y File Exchange.

Productos

Versión

R2023b

Preguntada:

el 24 de Feb. de 2024

Comentada:

el 25 de Feb. de 2024

Community Treasure Hunt

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

Start Hunting!

Translated by