Giter Club home page Giter Club logo

Comments (16)

blubbafett avatar blubbafett commented on May 31, 2024

Btw, I've also (just) started working on a surfaceOpts function.Will try to do more the coming weekend - need to read more carefully in the pgfplots manual in order to find which of the options in MATLAB that have equivalents in pgfplots.

% =========================================================================
% *** FUNCTION surfaceOpts
% =========================================================================
function [m2t,surfOpts,plotType] = surfaceOpts( m2t, handle )

  faceColor = get( handle, 'FaceColor');
  edgeColor = get( handle, 'EdgeColor');

  % Check for surf or mesh plot. Second argument in if-check corresponds to
  % default values for mesh plot in MATLAB.
  if strcmpi( faceColor, 'none') || ( strcmpi( edgeColor, 'flat' ) && isequal( faceColor, [1 1 1]))
      plotType = 'mesh';
  else
      plotType = 'surf';
  end

  surfOptions = cell(0);

  % Set opacity if FaceAlpha < 1 in MATLAB
  faceAlpha = get( handle, 'FaceAlpha');
  if faceAlpha ~= 1 && isnumeric( faceAlpha )
    surfOptions = appendOptions( surfOptions, sprintf( 'opacity=%g', faceAlpha ) );
  end

  if strcmpi( plotType, 'surf' )
    % Set shader for surface plot. 
    % TODO: find MATLAB equivalents for flat corner and flat mean  
    if strcmpi( edgeColor, 'none' ) && strcmpi( faceColor, 'flat' )
        surfOptions = appendOptions( surfOptions, sprintf( 'shader=flat' ) );
    elseif isnumeric( edgeColor) && strcmpi( faceColor, 'flat' )
        [ m2t, xEdgeColor ] = getColor( m2t, handle, edgeColor, 'patch' );
        % same as shader=flat,draw=\pgfkeysvalueof{/pgfplots/faceted color}
        surfOptions = appendOptions( surfOptions, sprintf( 'shader=faceted' ) );
        surfOptions = appendOptions( surfOptions, sprintf( 'draw=%s', xEdgeColor) );
    elseif strcmpi( edgeColor, 'none') && strcmpi( faceColor, 'interp' )
        surfOptions = appendOptions( surfOptions, sprintf( 'shader=interp') );
    else
        surfOptions = appendOptions( surfOptions, sprintf( 'shader=faceted' ) );
    end
    surfOpts = collapse( surfOptions , ',\n' );
  else % default for mesh plot is shader=flat
    surfOptions = appendOptions( surfOptions, sprintf( 'shader=flat' ) );
    surfOpts = collapse( surfOptions , ',\n' );
  end

end
% =========================================================================
% *** END FUNCTION surfaceOpts
% =========================================================================

with this, drawSurface() can be changed to

% =========================================================================
% *** FUNCTION drawSurface
% =========================================================================
function [m2t,env] = drawSurface( m2t, handle )

    str = [];
    [m2t, opts, plotType] = surfaceOpts( m2t, handle );
    str = [ str, sprintf( [ '\n\\addplot3[%%\n%s,\n', opts ,']\ncoordinates{ \n' ], plotType ) ];

    dx = get(handle,'XData');
    dy = get(handle,'YData');
    dz = get(handle,'ZData');
    [col, row] = size(dz);

    % check, if surf plot is 'spectrogram' or 'surf' and run corresponding
    % algorithm.
    if isvector(dx)
        % - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
        % plot is 'spectrogram'
        for i = 1:col
            for j = 1:row
                str = [ str, ...
                        sprintf('(%g,%g,%g)', dx(j), dy(i), dz(i,j) ) ];
            end
            % insert an empty line to tell Pgfplots about one row ending here
            str = [str, sprintf('\n\n')];
        end
        % - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    else
        % - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
        % plot is 'surf'
        for i = 1:col
            for j = 1:row
                str = [ str, ...
                        sprintf('(%g,%g,%g)', dx(i,j), dy(i,j), dz(i,j) ) ];
            end
            % insert an empty line to tell Pgfplots about one row ending here
            str = [str, sprintf('\n\n')];
        end
        % - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    end %if-else

% TODO:
% - remove grids in spectrogram by either removing grid command
%   or adding: 'grid=none' from/in axis options
% - using a "real" colorbar instead of colorbar-png-flle
% - handling of huge data amounts in LaTeX.
% - correcting wrong colors

str = [str, sprintf('};\n\n')];
env = str;

end
% =========================================================================
% *** END FUNCTION drawSurface
% =========================================================================

and things start to fall in place for 3D plots.

from matlab2tikz.

blubbafett avatar blubbafett commented on May 31, 2024

Since I'm obviously stupid and can't get the pull-request to work:

% =========================================================================
% *** FUNCTION getColorMap
% =========================================================================
function [m2t,cmapOpts] = getColorMap( m2t, handle )

  cm = get( gcf, 'Colormap' );
  [M N] = size(cm);
  cmap = cell(0);
  for k = 1:M
      [m2t, xtempColor] = getColor(m2t, handle, cm(k,:), 'path');
      cmap = appendOptions( cmap, sprintf( 'color=(%s)', xtempColor ) );
  end
  cmapOpts = ['colormap={mymap}{' collapse( cmap , '; ') '}' ];

end
% =========================================================================
% *** END FUNCTION getColorMap
% =========================================================================

to handle ColorMaps of surface/mesh plots. The only thing I fully haven't understood with the syntax is how I get to put options from this function inbetween the [] for \begin{axis}, i.e.

\begin{axis}[
    how to store options here??
]

Atm I've just used a "brute force" hack under the drawAxes() function, adding the following lines in an if-check, checking if the plot type is surface or line:

[m2t,colorMap] = surfaceColorMap( m2t, handle );
env.options = appendOptions( env.options, colorMap );

But I assume this isn't the way to go ;)

D

from matlab2tikz.

blubbafett avatar blubbafett commented on May 31, 2024

The drawColorbar function can also be altered to produce a PGF colorbar quite easily (haven't had time to dig into all the features PGF provides, but stil, shouldn't bee too hard). Also, there are still parts of the original code which I haven't removed which probably can be.

function [ m2t, env ] = drawColorbar( m2t, handle, alignmentOptions)

  if ~isVisible( handle )
      return
  end

  % the actual contents of the TikZ file go here
  env = struct( 'name',     'axis', ...
                'comment',  'colorbar', ...
                'options',  [], ...
                'content',  [], ...
                'children', []  ...
              );
  % Setting the following to cell(0) straight away doesn't work unfortunately
  % as MATLAB(R) interprets structs with cell values as a cell array of structs.
  env.options  = cell(0);
  env.content  = cell(0);
  env.children = cell(0);

  if ~isfield( m2t, 'colorbarNo' )
      % Keep track of how many colorbars there are, to avoid
      % file name collision in case PNGs are used.
      m2t.colorbarNo = [];
  end

  % Assume that the parent axes pair is m2t.currentHandles.gca.
  try
      m2t.currentHandles.gca;
  catch
      error( [ 'm2t.currentHandles.gca not set although needed ', ...
               'by the color bar. The parent axes have not been printed yet.' ] ...
           )
  end

  % begin collecting axes options
  cbarOptions = cell( 0 );
  cbarOptions = [ cbarOptions, 'axis on top' ];

  % - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  % set position, ticks etc. of the colorbar
  loc = get( handle, 'Location' );

  switch lower( loc )
      case { 'north', 'south', 'east', 'west' }
          userWarning( m2t, 'Don''t know how to deal with inner colorbars yet.' );
          return;

      case {'northoutside','southoutside'}
          if strcmpi(  loc , 'northoutside' )
              cbarOptions = [ cbarOptions 'colorbar horizontal' ...
                              'colorbar style={at={(0.5,1.1)}' ...     
                              'anchor=south' ...
                              'xticklabel pos=upper}' ];
          else
              cbarOptions = [ cbarOptions 'colorbar horizontal' ];
          end

      case {'eastoutside','westoutside'}
          if strcmpi(  loc , 'eastoutside' )
              cbarOptions = [ cbarOptions 'colorbar right' ];
           else
              cbarOptions = [ cbarOptions 'colorbar left' ];
           end

      otherwise
          error( 'drawColorbar: Unknown ''Location'' %s.', loc )
  end
  % set the options
  env.options = appendOptions( env.options, cbarOptions );

end
% =========================================================================
% *** END FUNCTION drawColorbar
% =========================================================================

BUT, all this needs again to be placed under the original axis environment, which I still haven't figured out (starting to get too many functions in a single file for me I think).

So far only the basic functionalities of the colorbar are included. I didn't find out if it is possible to change the width of the colorbar in MATLAB (which is possible in PGF, quite easily actually), so if MATLAB doesn't have this possibility, maybe this can be left out for the user to tweak after generating the tikz file.

from matlab2tikz.

nschloe avatar nschloe commented on May 31, 2024

Hi Dragan,

this is all good stuff! Let me go through it in the next few days and make sure it gets merged. It would make things even easier for me to have pull requests as then I get a nice diff of the the exact changes. If you don't get this to work though I'll just go through the inline code you submitted in the previous comments.

from matlab2tikz.

blubbafett avatar blubbafett commented on May 31, 2024

I think I've figured out the pull-request now. But could you let me know which object I should add the options from one nested function to in order to place the results within \begin{axis}\end{axis} environment of a plot. As I mentioned, I do this now by calling the nested functions in the drawAxes() function after running some checks, but I assume there's a more elegant way :)

from matlab2tikz.

nschloe avatar nschloe commented on May 31, 2024

Sure -- I'll look at it and let you know in a bit.

from matlab2tikz.

nschloe avatar nschloe commented on May 31, 2024

About adding options to the axis environment:
There is a struct 'env' defined in drawAxes() in which you could add stuff via

env.options = appendOptions( env.options, sprintf( 'xlabel={%s}', xlabelText ) );

or any old string. You had this figured out already. If you need to add something to the axis enviroment option from further below in the call stack i wouldn't mind either passing the 'env' down or the options up and adding somewhere with an appendOptions call like you suggested.

from matlab2tikz.

blubbafett avatar blubbafett commented on May 31, 2024

Ok, then I'll try the pull-request thing later today. Just need to go through the code again and check for errors.
dm

from matlab2tikz.

nschloe avatar nschloe commented on May 31, 2024

I'll also check for errors of course. A good way to check functionality would be to add test functions to the test/ directory. Then matlab2tikz_acidtest() runs all the test cases and produces original PDF and TikZed output and merges it automatically into a LaTeX file that then just needs to be compiled to get a neat face to face comparison of MATLAB(R) and matlab2tikz output. If those run as expected, one can be reasonably sure that the code is good.

from matlab2tikz.

blubbafett avatar blubbafett commented on May 31, 2024

Ah, that sounds nice! To be honest I hadn't check that opportunity yet. But I'll take a look at it later today - together with more options for the colorbar.

from matlab2tikz.

nschloe avatar nschloe commented on May 31, 2024

I've added a small note on how to run the tests on https://github.com/nicki/matlab2tikz/wiki/Contributing

from matlab2tikz.

blubbafett avatar blubbafett commented on May 31, 2024

Experience some problems with the test when testing

imagesc([1 10 100]);
colorbar('YScale','log')

Might be that such images can be drawn with "draw,fill=color" in Pgf, but I have to look a bit more into it first. Also have some problems with scaling on the (x/y) ticks in the colorbar with my version of the drawColorbar. But getting closer. So the pull request will most likely be delayed a bit, but I'll keep working and see if I can find a solution first :)

from matlab2tikz.

nschloe avatar nschloe commented on May 31, 2024

You can also split your pull request in different chunks: The label thing should be pretty independent of the colorbar scaling for example. That'll make it easier for me to review, too! :)

from matlab2tikz.

blubbafett avatar blubbafett commented on May 31, 2024

OK, now I think I screwed up with the pull-request thing. If I edit the file contents in my own fork, will it alter the code in your repository? If so I have to clean up my mess hehe

from matlab2tikz.

nschloe avatar nschloe commented on May 31, 2024

So I just pulled in your changes for the z-axis and the 3D plot handling and am really happy about it. Nicely done! The only thing missing now, as far as I can see, is the colormap stuff.

from matlab2tikz.

blubbafett avatar blubbafett commented on May 31, 2024

Ah, nice! Sorry about my absence the past week. Some urgent matters came up at the uni (my supervisor wants some equipment at our RF lab up and running before he's off to a conference in some weeks), so need to prioritize that the coming week(s). But will definitely look more into the other stuff I started on as soon as I get more spare time!

*Edit: And I should start making sure I click the correct button when posting a comment! ;)

from matlab2tikz.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.