What do these codes do?

3 ビュー (過去 30 日間)
Hind Aljallaf
Hind Aljallaf 2023 年 3 月 28 日
回答済み: Walter Roberson 2023 年 3 月 28 日
What do these codes do?
function [trainFiles] = URL_featureExt_AI(urlnames)
data_train_string = string(urlnames);
for i = 1:length(urlnames)
and also this code?
disp((i/length(urlnames))*100);

回答 (1 件)

Walter Roberson
Walter Roberson 2023 年 3 月 28 日
function [trainFiles] = URL_featureExt_AI(urlnames)
That declares that you are starting the definition of a function. The function name is URL_featureExt_AI .
It expects zero or one input parameters. If an input parameter is passed when the function is invoked, then the contents that are passed in will be available under the variable name urlnames . If no input parameter is passed in, then you could potentially check that by checking nargin < 1 or checking if ~exist('urlnames', 'var')
The function expects to output one output. Whatever the funtion assigns to the variable trainFiles will be made available as the first output to the code that called the function. If nothing is assigned to trainFiles in a context where the calling code expects at least one output, then there would be an error. If nothing is assigned to trainFiles in a context where the calling code is ignorning outputs, then there would not be an error.
data_train_string = string(urlnames);
As discussed above, zero or one inputs might have been passed to the function. This line assumes that at least one input was passed. If no inputs were passed, then MATLAB will detect that the variable urlnames requested here is the same as the name of an input parameter, and will generate an error message telling the user that they did not pass enough parameters to the function.
If the user did pass a parameter to the function, then the call to string() will attempt to convert each element of urlnames into a string() object. This is straight forward if a cell array of character vectors was passed -- the contents of the character vectors are just wrapped inside string() objects. If, however, numeric values were passed in to string() then string() will format the values as text vectors with 5 significant digits if the values have absolute value less than 1, or to have at most four digits after the decimal point for numeric values with absolute value 1 or higher. For example if the input was 3.2523235 then string() would convert that to the text "3.2523" . The output from string() will be a string() array.
for i = 1:length(urlnames)
length(urlnames) is defined as being:
  • 0 if any dimension of the variable urlnames is 0
  • otherwise the size of the largest dimension of urlnames . So for example if the user passed in a 2 x 3 x 2 cell array of character vectors then string() would have converted that to a 2 x 3 x 2 string() array, and max([2 3 2]) is 3 so length() of that would be 3. length(urlnames) is only the number of elements in urlnames under the special circumstances that urlnames was empty or that urlnames was a row vector or column vector. The use of length in a context such as this is often an error; often numel should be used instead.
Once the length of urlnames is determined, for will store away that value in an internal variable (not accessible to you) . That internal variable will not change even if urlnames is altered during the execution of the for loop.
If length(urlnames) is 0 (because the original urlnames is empty) then the 1:0 will evaluate to the empty array, [], and in that special case, the for loop would skip everything to the matching end statement and would continue after that with the loop variable i having been assigned [] (the empty array)
If length(urlnames) is not zero, then for will loop assigning i 1 the first time, 2 the second time, 3 the third time, and so on until the (stored) length of urlnames is reached. The loop will execute on the iteration where i becomes exactly equal to the stored length; in most cases, after the end of the loop, the loop control variable i will be left the last value it was assigned, the stored length of urlnames ( not 1 more than that value.)

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by