ask user whether to delete zeros in data or replace zeros with mean of data in matlab

You can use the input function to ask the user which operation they want to perform. Here is a sample code to achieve the required functionality:

main.m
% Sample data
data = [1 2 0 4 5 0 7 8 0];

% Ask user input to choose operation
op = input('Enter operation (1: delete zeros, 2: replace zeros with mean of data): ');

switch op
    case 1
        % Delete zeros from data
        new_data = data(data~=0);
    case 2
        % Replace zeros with mean of data
        mean_data = mean(data(data~=0));
        new_data = data;
        new_data(new_data==0) = mean_data;
    otherwise
        % Invalid input
        disp('Invalid input');
end
491 chars
20 lines

In this code, we first define a sample data array. We then use the input function to ask the user which operation they want to perform. We store the user input in the variable op.

We then use a switch statement to perform the selected operation. If the user selects option 1, we delete zeros from the data using logical indexing. If the user selects option 2, we replace zeros with the mean of data using logical indexing and then assign the mean to the zeros.

If the user inputs an invalid option, we display a message saying "Invalid input".

gistlibby LogSnag