Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3354744
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T02:18:38+00:00 2026-05-18T02:18:38+00:00

In MATLAB, I’m using fprintf to print a list of numerical values under column

  • 0

In MATLAB, I’m using fprintf to print a list of numerical values under column headings, like so:

fprintf('%s %s %s %s\n', 'col1', 'col2', 'col3', 'col4')
for i = 1:length(myVar)
    fprintf('%8.4g %8.4g %8.4g %8.4g\n', myVar{i,1}, myVar{i,2}, myVar{i,3}, myVar{i,4})
end

This results in something like this:

    col1     col2     col3     col4
   123.5    234.6    345.7    456.8

However, when one of the cells is empty (e.g. myVar{i,3} == []), space is not preserved:

    col1     col2     col3     col4
   123.5    234.6     456.8

How do I preserve space in my format for a numerical value that may be empty?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T02:18:39+00:00Added an answer on May 18, 2026 at 2:18 am

    One option is to use the functions CELLFUN and NUM2STR to change each cell to a string first, then print each cell as a string using FPRINTF:

    fprintf('%8s %8s %8s %8s\n', 'col1', 'col2', 'col3', 'col4');
    for i = 1:size(myVar,1)
      temp = cellfun(@(x) num2str(x,'%8.4g'),myVar(i,:),'UniformOutput',false);
      fprintf('%8s %8s %8s %8s\n',temp{:});
    end
    

    This should give you output like:

        col1     col2     col3     col4
       123.5    234.6             456.8
    

    Notice also that I added eights to your first FPRINTF call to fix the formatting of the column labels and changed length(myVar) to size(myVar,1) since you are looping over the rows of myVar.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my matlab m-file I am using some logic (string concat) to build variables
In matlab there is a way to find the values in one vector but
In Matlab, there is a very nice feature that I like. Suppose I typed
How does MATLAB's round function work with .5? Like 5.5? Does it round up
In Matlab, I would like to update the data plotted in a set of
Does MATLAB have a blackhole or discard variable? Say I'm doing something like: [
Using matlab 2010 with NET.addAssembly(MyName), how do I check whether certain assembly MyName was
In Matlab, I recall being able to declare an array and initialize it and
I see in the MATLAB help ( matlab -h ) that I can use
I'm porting some MATLAB functions to Scilab . The cool thing is that there

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.