This erro occurred while cheking the code - "invalid use of operator"

6 vues (au cours des 30 derniers jours)
Valik
Valik le 28 Fév 2025
function ['input_file', 'output_file'] = min_max_normalization('input_file', 'output_file')
% Завантаження даних
data = load('input_file.m');
x_data = fieldnames(data);
x = data.(x_data{1});
A = 0;
B = 1;
x_min = min(x);
x_max = max(x);
x_norm = ((x - x_min) / (x_max - x_min)) * (B - A) + A;
% Збереження у вихідний файл
save('output_file.m', 'x_norm');
end
mat-file "input_file.m"
K = [97 82 94 87 94 95 84 86 86 95 97 91 87 91 85 92 98 84 95 95];
save("input_file","K");
mat-file "output_file.m"
empty

Réponses (2)

Les Beckham
Les Beckham le 28 Fév 2025
This line is not valid Matlab syntax and was causing your error message
mat-file "input_file.m"
Maybe this is what you are trying to do???
K = [97 82 94 87 94 95 84 86 86 95 97 91 87 91 85 92 98 84 95 95];
save('input_file.mat', 'K') % <<<<<< NOTE: .mat files save data, .m files contain code
min_max_normalization %<<<< Call the function!!! See below for results
I'm not sure why you are saving K and x_norm into mat files.
A better way to do this is just pass the input data into the function and return the output data as the output argument. Like this:
returnValue = new_min_max_normalization(K)
returnValue = 1×20
0.9375 0 0.7500 0.3125 0.7500 0.8125 0.1250 0.2500 0.2500 0.8125 0.9375 0.5625 0.3125 0.5625 0.1875 0.6250 1.0000 0.1250 0.8125 0.8125
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
% If you are reading/writing data from .mat files with hard-coded names
% your function doesn't need any input arguments or output arguments
% function ['input_file', 'output_file'] = min_max_normalization('input_file', 'output_file')
function min_max_normalization()
% Завантаження даних
data = load('input_file.mat');
x_data = fieldnames(data);
x = data.(x_data{1});
A = 0;
B = 1;
x_min = min(x);
x_max = max(x);
x_norm = ((x - x_min) / (x_max - x_min)) * (B - A) + A;
% Збереження у вихідний файл
save('output_file.mat', 'x_norm');
end
% check if output_file.m was saved with the right data
dir *.mat
input_file.mat output_file.mat
whos -file output_file.mat
Name Size Bytes Class Attributes x_norm 1x20 160 double
function [x_norm] = new_min_max_normalization(x)
A = 0;
B = 1;
x_min = min(x);
x_max = max(x);
x_norm = ((x - x_min) / (x_max - x_min)) * (B - A) + A;
end
  1 commentaire
Les Beckham
Les Beckham le 28 Fév 2025
Also, if you are just getting started with Matlab, I would highly recommend that you take a couple of hours to go through the free online tutorial: Matlab Onramp

Connectez-vous pour commenter.


Image Analyst
Image Analyst le 1 Mar 2025
Don't put single quotes around your variables. And you don't need the output arguments of the filenames. You can return the x_norm if you want, otherwise you just have to know it's saved in the mat file.
function x_norm = min_max_normalization(input_file_name)
% Save a normalized version of the input data
% in an output file called "output_file.mat"
x_norm = []; % Initialize an output variable. Needed in case input file does not exist.
% Завантаження даних
if ~isfile(input_file_name)
% If file does not exist, exit.
warningMessage = sprintf('Warning: Input mat file not found:\n%s', input_file_name);
uiwait(warndlg(warningMessage));
return;
end
% File exists if you get here. (I'm assuming the code below to get x works)
data = load(input_file_name) % No single quotes here!
x_data = fieldnames(data)
x = data.(x_data{1});
A = 0;
B = 1;
x_min = min(x);
x_max = max(x);
x_norm = ((x - x_min) / (x_max - x_min)) * (B - A) + A;
% Збереження у вихідний файл
save('output_file.mat', 'x_norm'); % Note: you do want quotes here but the file extension should be .mat.
end
Note that this
A = 0;
B = 1;
x_min = min(x);
x_max = max(x);
x_norm = ((x - x_min) / (x_max - x_min)) * (B - A) + A;
can be done more simply like this:
x_norm = rescale(x, 0, 1);

Catégories

En savoir plus sur GPU Computing dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by