How to create automatic new line?
Can someone help me create an anonymous function format_this(txt)
to format text so there's a newline character replacing a whitespace that is close to the edge of the command window - in essence 'pretty printing'? It does not have to be perfect (and in fact, doesn't need to be an anonymous function), however I couldn't find something like that oddly enough...
Here's what I have:
txt='the quick brown fox jumps over the lazy dog';
txt=[txt ' ' txt ' ' txt]; %make longer
w=getfield(get(0,'CommandWindowSize'),{1}); %command window width
space_pos=strfind(txt,' '); %find space positions
wrap_x_times= (w:w:size(txt,2))); %estimate of many times the text should wrap to a newline
format_this=@(txt) txt;
%something like an ideal output:
disp(format_this(txt)) %example for super-small window
ans =
'the quick brown fox jumps over the lazy dog
the quick brown fox jumps over the lazy dog
the quick brown fox jumps over the lazy dog'
regex matlab
add a comment |
Can someone help me create an anonymous function format_this(txt)
to format text so there's a newline character replacing a whitespace that is close to the edge of the command window - in essence 'pretty printing'? It does not have to be perfect (and in fact, doesn't need to be an anonymous function), however I couldn't find something like that oddly enough...
Here's what I have:
txt='the quick brown fox jumps over the lazy dog';
txt=[txt ' ' txt ' ' txt]; %make longer
w=getfield(get(0,'CommandWindowSize'),{1}); %command window width
space_pos=strfind(txt,' '); %find space positions
wrap_x_times= (w:w:size(txt,2))); %estimate of many times the text should wrap to a newline
format_this=@(txt) txt;
%something like an ideal output:
disp(format_this(txt)) %example for super-small window
ans =
'the quick brown fox jumps over the lazy dog
the quick brown fox jumps over the lazy dog
the quick brown fox jumps over the lazy dog'
regex matlab
I will post a solution if no answers, but since I had a hard time finding this, maybe the answer will be of use to others
– user2305193
Jan 2 at 4:48
@tripleee it's matlab, I didn't find a textwrap function, here's a longwinded function link. It's not homework, it's something that just annoyed me. I also like elegant code.
– user2305193
Jan 2 at 5:32
Sorry, missed the Matlab requirement. Quick googling gets me mathworks.com/matlabcentral/fileexchange/53176-wraptext
– tripleee
Jan 2 at 5:42
add a comment |
Can someone help me create an anonymous function format_this(txt)
to format text so there's a newline character replacing a whitespace that is close to the edge of the command window - in essence 'pretty printing'? It does not have to be perfect (and in fact, doesn't need to be an anonymous function), however I couldn't find something like that oddly enough...
Here's what I have:
txt='the quick brown fox jumps over the lazy dog';
txt=[txt ' ' txt ' ' txt]; %make longer
w=getfield(get(0,'CommandWindowSize'),{1}); %command window width
space_pos=strfind(txt,' '); %find space positions
wrap_x_times= (w:w:size(txt,2))); %estimate of many times the text should wrap to a newline
format_this=@(txt) txt;
%something like an ideal output:
disp(format_this(txt)) %example for super-small window
ans =
'the quick brown fox jumps over the lazy dog
the quick brown fox jumps over the lazy dog
the quick brown fox jumps over the lazy dog'
regex matlab
Can someone help me create an anonymous function format_this(txt)
to format text so there's a newline character replacing a whitespace that is close to the edge of the command window - in essence 'pretty printing'? It does not have to be perfect (and in fact, doesn't need to be an anonymous function), however I couldn't find something like that oddly enough...
Here's what I have:
txt='the quick brown fox jumps over the lazy dog';
txt=[txt ' ' txt ' ' txt]; %make longer
w=getfield(get(0,'CommandWindowSize'),{1}); %command window width
space_pos=strfind(txt,' '); %find space positions
wrap_x_times= (w:w:size(txt,2))); %estimate of many times the text should wrap to a newline
format_this=@(txt) txt;
%something like an ideal output:
disp(format_this(txt)) %example for super-small window
ans =
'the quick brown fox jumps over the lazy dog
the quick brown fox jumps over the lazy dog
the quick brown fox jumps over the lazy dog'
regex matlab
regex matlab
edited Jan 2 at 5:08
user2305193
asked Jan 2 at 4:46
user2305193user2305193
861622
861622
I will post a solution if no answers, but since I had a hard time finding this, maybe the answer will be of use to others
– user2305193
Jan 2 at 4:48
@tripleee it's matlab, I didn't find a textwrap function, here's a longwinded function link. It's not homework, it's something that just annoyed me. I also like elegant code.
– user2305193
Jan 2 at 5:32
Sorry, missed the Matlab requirement. Quick googling gets me mathworks.com/matlabcentral/fileexchange/53176-wraptext
– tripleee
Jan 2 at 5:42
add a comment |
I will post a solution if no answers, but since I had a hard time finding this, maybe the answer will be of use to others
– user2305193
Jan 2 at 4:48
@tripleee it's matlab, I didn't find a textwrap function, here's a longwinded function link. It's not homework, it's something that just annoyed me. I also like elegant code.
– user2305193
Jan 2 at 5:32
Sorry, missed the Matlab requirement. Quick googling gets me mathworks.com/matlabcentral/fileexchange/53176-wraptext
– tripleee
Jan 2 at 5:42
I will post a solution if no answers, but since I had a hard time finding this, maybe the answer will be of use to others
– user2305193
Jan 2 at 4:48
I will post a solution if no answers, but since I had a hard time finding this, maybe the answer will be of use to others
– user2305193
Jan 2 at 4:48
@tripleee it's matlab, I didn't find a textwrap function, here's a longwinded function link. It's not homework, it's something that just annoyed me. I also like elegant code.
– user2305193
Jan 2 at 5:32
@tripleee it's matlab, I didn't find a textwrap function, here's a longwinded function link. It's not homework, it's something that just annoyed me. I also like elegant code.
– user2305193
Jan 2 at 5:32
Sorry, missed the Matlab requirement. Quick googling gets me mathworks.com/matlabcentral/fileexchange/53176-wraptext
– tripleee
Jan 2 at 5:42
Sorry, missed the Matlab requirement. Quick googling gets me mathworks.com/matlabcentral/fileexchange/53176-wraptext
– tripleee
Jan 2 at 5:42
add a comment |
2 Answers
2
active
oldest
votes
You need a combination of string functions to achieve that result. The program below shows how to do that.
clc
% the text
txt='the quick brown fox jumps over the lazy dog';
% makethe text a bit longer
txt=[txt ' ' txt ' ' txt];
% get the command window width
w=getfield(get(0,'CommandWindowSize'),{1});
% get the length of the text
txt_len = numel(txt);
% check if the length of text is exactly divisible
% by the size of window (w) or not
if(mod(txt_len, w)~= 0)
% if not, then get the number of
% characters required to make it a
% multiple of w
diff_n = w - mod(txt_len, w);
% append that many spaces to the end of the string
txt(end+1 : end+diff_n) = ' ';
end
% create an anoymous function
% step 1 - Split the array in multiple of size w into a cell array
% using reshape() and cellstr() function respectively
% step 2 - concatenate the newline character n at the end of each
% element of the cell array using strcat()
% step 4 - join the cell array elements in a single string usin join()
format_this = @(txt)join(strcat(cellstr(reshape(txt,w, )'), 'n'));
% get the formatted string as a 1-d cell array
formatted_str = format_this(txt);
% print the string to ft
ft = sprintf(formatted_str{1});
% display the ft
disp(ft)
Program Output tested with variable size of the command window.
2
You can take a couple of shortcuts in your code... There's no real need for theif
statement, as something liketxt(end+1:end+0)
is empty, so will do nothing if there's no remainder. The anonymous function is a bit surplus as it requires the previous setup... unless you can do the whitespace padding inside the function, it might as well operate directly ontxt
. You can usefprintf
instead ofsprintf
withdisp
. Your code does appear to work as desired though, these are just pointers for reference :)
– Wolfie
Jan 2 at 14:15
Yes, I am aware of the facts. I am not as experienced like the experts here e.g., you. That's the reason I solve the problems here with basic long method some times. But gradually I will reach there to the expert level, I know. :)
– Anjan
Jan 2 at 14:40
add a comment |
For printing in the Command Window, this is a preference which can be set in the preferences pane under
HOME > Preferences > Command Window
The result can be seen with a quick test:
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54001314%2fhow-to-create-automatic-new-line%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You need a combination of string functions to achieve that result. The program below shows how to do that.
clc
% the text
txt='the quick brown fox jumps over the lazy dog';
% makethe text a bit longer
txt=[txt ' ' txt ' ' txt];
% get the command window width
w=getfield(get(0,'CommandWindowSize'),{1});
% get the length of the text
txt_len = numel(txt);
% check if the length of text is exactly divisible
% by the size of window (w) or not
if(mod(txt_len, w)~= 0)
% if not, then get the number of
% characters required to make it a
% multiple of w
diff_n = w - mod(txt_len, w);
% append that many spaces to the end of the string
txt(end+1 : end+diff_n) = ' ';
end
% create an anoymous function
% step 1 - Split the array in multiple of size w into a cell array
% using reshape() and cellstr() function respectively
% step 2 - concatenate the newline character n at the end of each
% element of the cell array using strcat()
% step 4 - join the cell array elements in a single string usin join()
format_this = @(txt)join(strcat(cellstr(reshape(txt,w, )'), 'n'));
% get the formatted string as a 1-d cell array
formatted_str = format_this(txt);
% print the string to ft
ft = sprintf(formatted_str{1});
% display the ft
disp(ft)
Program Output tested with variable size of the command window.
2
You can take a couple of shortcuts in your code... There's no real need for theif
statement, as something liketxt(end+1:end+0)
is empty, so will do nothing if there's no remainder. The anonymous function is a bit surplus as it requires the previous setup... unless you can do the whitespace padding inside the function, it might as well operate directly ontxt
. You can usefprintf
instead ofsprintf
withdisp
. Your code does appear to work as desired though, these are just pointers for reference :)
– Wolfie
Jan 2 at 14:15
Yes, I am aware of the facts. I am not as experienced like the experts here e.g., you. That's the reason I solve the problems here with basic long method some times. But gradually I will reach there to the expert level, I know. :)
– Anjan
Jan 2 at 14:40
add a comment |
You need a combination of string functions to achieve that result. The program below shows how to do that.
clc
% the text
txt='the quick brown fox jumps over the lazy dog';
% makethe text a bit longer
txt=[txt ' ' txt ' ' txt];
% get the command window width
w=getfield(get(0,'CommandWindowSize'),{1});
% get the length of the text
txt_len = numel(txt);
% check if the length of text is exactly divisible
% by the size of window (w) or not
if(mod(txt_len, w)~= 0)
% if not, then get the number of
% characters required to make it a
% multiple of w
diff_n = w - mod(txt_len, w);
% append that many spaces to the end of the string
txt(end+1 : end+diff_n) = ' ';
end
% create an anoymous function
% step 1 - Split the array in multiple of size w into a cell array
% using reshape() and cellstr() function respectively
% step 2 - concatenate the newline character n at the end of each
% element of the cell array using strcat()
% step 4 - join the cell array elements in a single string usin join()
format_this = @(txt)join(strcat(cellstr(reshape(txt,w, )'), 'n'));
% get the formatted string as a 1-d cell array
formatted_str = format_this(txt);
% print the string to ft
ft = sprintf(formatted_str{1});
% display the ft
disp(ft)
Program Output tested with variable size of the command window.
2
You can take a couple of shortcuts in your code... There's no real need for theif
statement, as something liketxt(end+1:end+0)
is empty, so will do nothing if there's no remainder. The anonymous function is a bit surplus as it requires the previous setup... unless you can do the whitespace padding inside the function, it might as well operate directly ontxt
. You can usefprintf
instead ofsprintf
withdisp
. Your code does appear to work as desired though, these are just pointers for reference :)
– Wolfie
Jan 2 at 14:15
Yes, I am aware of the facts. I am not as experienced like the experts here e.g., you. That's the reason I solve the problems here with basic long method some times. But gradually I will reach there to the expert level, I know. :)
– Anjan
Jan 2 at 14:40
add a comment |
You need a combination of string functions to achieve that result. The program below shows how to do that.
clc
% the text
txt='the quick brown fox jumps over the lazy dog';
% makethe text a bit longer
txt=[txt ' ' txt ' ' txt];
% get the command window width
w=getfield(get(0,'CommandWindowSize'),{1});
% get the length of the text
txt_len = numel(txt);
% check if the length of text is exactly divisible
% by the size of window (w) or not
if(mod(txt_len, w)~= 0)
% if not, then get the number of
% characters required to make it a
% multiple of w
diff_n = w - mod(txt_len, w);
% append that many spaces to the end of the string
txt(end+1 : end+diff_n) = ' ';
end
% create an anoymous function
% step 1 - Split the array in multiple of size w into a cell array
% using reshape() and cellstr() function respectively
% step 2 - concatenate the newline character n at the end of each
% element of the cell array using strcat()
% step 4 - join the cell array elements in a single string usin join()
format_this = @(txt)join(strcat(cellstr(reshape(txt,w, )'), 'n'));
% get the formatted string as a 1-d cell array
formatted_str = format_this(txt);
% print the string to ft
ft = sprintf(formatted_str{1});
% display the ft
disp(ft)
Program Output tested with variable size of the command window.
You need a combination of string functions to achieve that result. The program below shows how to do that.
clc
% the text
txt='the quick brown fox jumps over the lazy dog';
% makethe text a bit longer
txt=[txt ' ' txt ' ' txt];
% get the command window width
w=getfield(get(0,'CommandWindowSize'),{1});
% get the length of the text
txt_len = numel(txt);
% check if the length of text is exactly divisible
% by the size of window (w) or not
if(mod(txt_len, w)~= 0)
% if not, then get the number of
% characters required to make it a
% multiple of w
diff_n = w - mod(txt_len, w);
% append that many spaces to the end of the string
txt(end+1 : end+diff_n) = ' ';
end
% create an anoymous function
% step 1 - Split the array in multiple of size w into a cell array
% using reshape() and cellstr() function respectively
% step 2 - concatenate the newline character n at the end of each
% element of the cell array using strcat()
% step 4 - join the cell array elements in a single string usin join()
format_this = @(txt)join(strcat(cellstr(reshape(txt,w, )'), 'n'));
% get the formatted string as a 1-d cell array
formatted_str = format_this(txt);
% print the string to ft
ft = sprintf(formatted_str{1});
% display the ft
disp(ft)
Program Output tested with variable size of the command window.
answered Jan 2 at 11:46
AnjanAnjan
33917
33917
2
You can take a couple of shortcuts in your code... There's no real need for theif
statement, as something liketxt(end+1:end+0)
is empty, so will do nothing if there's no remainder. The anonymous function is a bit surplus as it requires the previous setup... unless you can do the whitespace padding inside the function, it might as well operate directly ontxt
. You can usefprintf
instead ofsprintf
withdisp
. Your code does appear to work as desired though, these are just pointers for reference :)
– Wolfie
Jan 2 at 14:15
Yes, I am aware of the facts. I am not as experienced like the experts here e.g., you. That's the reason I solve the problems here with basic long method some times. But gradually I will reach there to the expert level, I know. :)
– Anjan
Jan 2 at 14:40
add a comment |
2
You can take a couple of shortcuts in your code... There's no real need for theif
statement, as something liketxt(end+1:end+0)
is empty, so will do nothing if there's no remainder. The anonymous function is a bit surplus as it requires the previous setup... unless you can do the whitespace padding inside the function, it might as well operate directly ontxt
. You can usefprintf
instead ofsprintf
withdisp
. Your code does appear to work as desired though, these are just pointers for reference :)
– Wolfie
Jan 2 at 14:15
Yes, I am aware of the facts. I am not as experienced like the experts here e.g., you. That's the reason I solve the problems here with basic long method some times. But gradually I will reach there to the expert level, I know. :)
– Anjan
Jan 2 at 14:40
2
2
You can take a couple of shortcuts in your code... There's no real need for the
if
statement, as something like txt(end+1:end+0)
is empty, so will do nothing if there's no remainder. The anonymous function is a bit surplus as it requires the previous setup... unless you can do the whitespace padding inside the function, it might as well operate directly on txt
. You can use fprintf
instead of sprintf
with disp
. Your code does appear to work as desired though, these are just pointers for reference :)– Wolfie
Jan 2 at 14:15
You can take a couple of shortcuts in your code... There's no real need for the
if
statement, as something like txt(end+1:end+0)
is empty, so will do nothing if there's no remainder. The anonymous function is a bit surplus as it requires the previous setup... unless you can do the whitespace padding inside the function, it might as well operate directly on txt
. You can use fprintf
instead of sprintf
with disp
. Your code does appear to work as desired though, these are just pointers for reference :)– Wolfie
Jan 2 at 14:15
Yes, I am aware of the facts. I am not as experienced like the experts here e.g., you. That's the reason I solve the problems here with basic long method some times. But gradually I will reach there to the expert level, I know. :)
– Anjan
Jan 2 at 14:40
Yes, I am aware of the facts. I am not as experienced like the experts here e.g., you. That's the reason I solve the problems here with basic long method some times. But gradually I will reach there to the expert level, I know. :)
– Anjan
Jan 2 at 14:40
add a comment |
For printing in the Command Window, this is a preference which can be set in the preferences pane under
HOME > Preferences > Command Window
The result can be seen with a quick test:
add a comment |
For printing in the Command Window, this is a preference which can be set in the preferences pane under
HOME > Preferences > Command Window
The result can be seen with a quick test:
add a comment |
For printing in the Command Window, this is a preference which can be set in the preferences pane under
HOME > Preferences > Command Window
The result can be seen with a quick test:
For printing in the Command Window, this is a preference which can be set in the preferences pane under
HOME > Preferences > Command Window
The result can be seen with a quick test:
answered Jan 2 at 11:42
WolfieWolfie
16.5k51745
16.5k51745
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54001314%2fhow-to-create-automatic-new-line%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
I will post a solution if no answers, but since I had a hard time finding this, maybe the answer will be of use to others
– user2305193
Jan 2 at 4:48
@tripleee it's matlab, I didn't find a textwrap function, here's a longwinded function link. It's not homework, it's something that just annoyed me. I also like elegant code.
– user2305193
Jan 2 at 5:32
Sorry, missed the Matlab requirement. Quick googling gets me mathworks.com/matlabcentral/fileexchange/53176-wraptext
– tripleee
Jan 2 at 5:42