3

How do you read multi-line input from clipboard at once?

I tried using input but line change appears treated as Enter pressing.

>> x=input(char.empty,'s');
2020
2037

2054
2131
ans =
        2037
ans =
        2054
ans =
        2131
>> x
x =
    '2020'

atm, I read from clipboard as a workaround. But it'd be nice to be able to copy-paste multi-line input to Matlab's environment and process that.

0

1 Answer 1

3

You can call the input function for each input line, in a loop. Since the number of lines may not be known in advance, you can use a predefined string to mark end of input:

x = {};
end_of_input = 'q';
done = false;
while ~done
    x{end+1} = input(char.empty,'s');
    done = strcmp(x{end}, end_of_input);
end
x(end) = [];

If you then need to convert non-empty lines to numbers:

x = str2double(x(~cellfun(@isempty, x)));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.